如何在C#中获取两个日期之间的所有日期
我正在编写一个代码,该代码从用户获取两个日期并为该用户预订一个插槽,然后datepicker应禁用该日期,以便其他用户无法在同一天预订。我想到将所有日期存储在数组中并使datepicker禁用数组中的所有日期。如何编写代码来存储数组中的日期。这是我的代码给出了一个错误,Listtype无法转换为字符串[]。
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[].
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);
}
您不能只处理List< DateTime>作为DateTime值的数组,它不止于此。你必须转换它,这很简单。但是 - 你不想使用字符串,在处理基于日期的信息时它们真的不能比较好!
有三种方法可以解决这个问题:
1)更改方法以返回DateTime值数组:
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:
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)在方法之外使用List:
2) Use a List outside the method:
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)将List转换为方法之外的数组:
3) Convert the List to an array outside the method:
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();
}
1)列表声明错误。
1) The list declaration was wrong.
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)您可以使用ToArray()方法将列表转换为数组
2) You can convert a list to an array by using the ToArray() method
DateTime[] dates = GetDatesBetween(starting, ending).ToArray();
我更正了你的代码。
I corrected your code.
用下面的代码更改你的代码。
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;
}
我希望您有使用datetimepicker禁用日期的代码。
I hope you have the code to disable Dates with datetimepicker.