Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I'm writing a code that takes two dates from the user and book a slot for that user and then the datepicker should disable that date so that other user cannot book the same day. I thought of storing all the dates in the array and making the datepicker disable all the dates in the array. How can I write my code To store The dates in array. Here Is my code which is giving me a error that Listtype cannot be converted into string[].
C#
public List<DateTime> GetDatesBetween(DateTime startDate, DateTime endDate)
    {
        List<DateTime> allDates = new List<DateTime>();
        for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
            allDates.Add(date);
        return allDates;

    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        DateTime starting=new DateTime();
       starting=DateTime.ParseExact(datepicker.Value,"dd-MM-yyyy",null);
        DateTime ending=new DateTime();
       ending=DateTime.ParseExact(date2.Value,"dd-MM-yyyy",null);


      String[] dates = GetDatesBetween(starting, ending);  
      }
Posted
Updated 16-Jun-21 20:45pm
v3

You can't just treat a List<DateTime> as an array of DateTime values, it's more than that. You have to convert it, which is simple to do. But - you don't want to use strings, they really don't compare well when dealing with date based info!
There are three ways to deal with this:
1) Change the method to return an array of DateTime values:
C#
public DateTime[] GetDatesBetween(DateTime startDate, DateTime endDate)
    {
    List<DateTime> allDates = new List<DateTime>();
    for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
    allDates.Add(date);
    return allDates.ToArray(); 
    }

2) Use a List outside the method:
C#
protected void Button1_Click1(object sender, EventArgs e)
    {
    DateTime starting=new DateTime();
    starting=DateTime.ParseExact(datepicker.Value,"dd-MM-yyyy",null);
    DateTime ending=new DateTime();
    ending=DateTime.ParseExact(date2.Value,"dd-MM-yyyy",null);
    
    List<DateTime> dates = GetDatesBetween(starting, ending); 
    }

3) Convert the List to an array outside the method:
C#
protected void Button1_Click1(object sender, EventArgs e)
    {
    DateTime starting=new DateTime();
    starting=DateTime.ParseExact(datepicker.Value,"dd-MM-yyyy",null);
    DateTime ending=new DateTime();
    ending=DateTime.ParseExact(date2.Value,"dd-MM-yyyy",null);
    
    DateTime[] dates = GetDatesBetween(starting, ending).ToArray(); 
    }
 
Share this answer
 
v2
Comments
Praveen Kumar Upadhyay 9-Dec-14 10:34am    
List allDates = new List();
Above will give compilation error as it is a generic and has to have a type with it. Please improve your solution.
TheRealSteveJudge 9-Dec-14 10:38am    
Might be a good idea!
OriginalGriff 9-Dec-14 10:45am    
Copy and paste from his original - fixed.
TheRealSteveJudge 9-Dec-14 10:35am    
Although brackets are missing at ToArray method: 5*
Praveen Kumar Upadhyay 9-Dec-14 10:45am    
No why I have mentioned that because OP was missing that too.
1) The list declaration was wrong.

C#
public List<DateTime> GetDatesBetween(DateTime startDate, DateTime endDate)
{
    List<DateTime> allDates = new List<DateTime>();

    for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
    {
        allDates.Add(date.Date);
    }

    return allDates;
}


2) You can convert a list to an array by using the ToArray() method

C#
DateTime[] dates = GetDatesBetween(starting, ending).ToArray();


I corrected your code.
 
Share this answer
 
v6
Change your code with below.

public List<DateTime> GetDatesBetween(DateTime startDate, DateTime endDate)
        {
            List<DateTime> allDates = new List<DateTime>();
            for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
                allDates.Add(date.Date);
            return allDates;
        }


I hope you have the code to disable Dates with datetimepicker.
 
Share this answer
 
Comments
Member 11239022 9-Dec-14 12:43pm    
NO can You please give that also
Using LINQ you can do the same with one line of code
C#
DateTime[] dates = Enumerable.Range(0, 1 + ending.Subtract(starting).Days)
          .Select(i=> starting.AddDays(i))
          .ToArray();

if you need string array
C#
string[] dates = Enumerable.Range(0, 1 + ending.Subtract(starting).Days)
          .Select(i=> starting.AddDays(i).ToString("dd-MM-yyyy"))
          .ToArray();
 
Share this answer
 
v4
Another approach with iterator pattern:
C#
private const static string DATEFORMAT = "dd-MM-yyyy";

C#
public static IEnumerable<string> GetRangeOfDays(DateTime startDate, DateTime endDate)
{
    int days = (endDate - startDate).Days;
    for(int d = 0; d <= days; d++)
    {
        yield return startDate.AddDays(d).ToString(DATEFORMAT);
    }
}
Using this:
C#
DateTime starting      = DateTime.ParseExact(datepicker.Value,DATEFORMAT,null);
DateTime ending        = DateTime.ParseExact(date2.Value,DATEFORMAT,null);
string[] datesAsString = GetRangeOfDays(starting, ending).ToArray();

Some hints:
- initialize the starting/ending variables with the value returned by the parsing method
- use iterator pattern (see also C# Iterator Pattern demystified[^]) to get a sequence of dates as stings
Cheers
Andi
 
Share this answer
 
vStr = " Select * from tbl_Record where Date >= Cdate (#" + txtSDate.DateTime.ToString("MM/dd/yyyy") + "#) and Date < Cdate (#" + txtEndDate.DateTime.AddDays(1).ToString("MM/dd/yyyy") + "#)";
 
Share this answer
 
Comments
Richard MacCutchan 17-Jun-21 3:41am    
Already answered more than six years ago. Please focus on current problems.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900