我想獲取當前週末的日期列表。 是有什麼辦法可以將所有周末的日期在我的名單如何獲得當前月份使用c#在asp.net中使用週末列表的日曆控件#
list<datetime> mylist=new list<datetime>();
我想週末日期存儲在當月的MYLIST並用相同未來幾個月 即時通訊使用asp.net壓延控制,我想將週末日期保存在我的列表中。
我想獲取當前週末的日期列表。 是有什麼辦法可以將所有周末的日期在我的名單如何獲得當前月份使用c#在asp.net中使用週末列表的日曆控件#
list<datetime> mylist=new list<datetime>();
我想週末日期存儲在當月的MYLIST並用相同未來幾個月 即時通訊使用asp.net壓延控制,我想將週末日期保存在我的列表中。
對於當月,你可以做
var startDate = DateTime.Now;
var first = new DateTime(startDate.Year, startDate.Month, 1);
List<DateTime> weekends = new List<DateTime>();
for (int i = 0; i <= DateTime.DaysInMonth(startDate.Year, startDate.Month); i++)
{
var nextDay = first.AddDays(i);
if (nextDay.DayOfWeek == DayOfWeek.Saturday || nextDay.DayOfWeek == DayOfWeek.Sunday)
{
weekends.Add(nextDay);
}
}
你可以做下個月的非常類似的東西。
重複此月的每週
DateTime date = DateTime.Now;
DateTime saturday;
DateTime sunday;
if (date.DayOfWeek == DayOfWeek.Monday)
{
saturday = date.AddDays(5);
sunday = date.AddDays(6);
}else if (date.DayOfWeek == DayOfWeek.Tuesday)
{
saturday = date.AddDays(4);
sunday = date.AddDays(5);
}
else if (date.DayOfWeek == DayOfWeek.Wednesday)
{
saturday = date.AddDays(3);
sunday = date.AddDays(4);
}
else if (date.DayOfWeek == DayOfWeek.Thursday)
{
saturday = date.AddDays(2);
sunday = date.AddDays(3);
}
else if (date.DayOfWeek == DayOfWeek.Friday)
{
saturday = date.AddDays(1);
sunday = date.AddDays(2);
}
else if (date.DayOfWeek == DayOfWeek.Saturday)
{
saturday = date.AddDays(0);
sunday = date.AddDays(1);
}
else if (date.DayOfWeek == DayOfWeek.Sunday)
{
saturday = date.AddDays(-1);
sunday = date.AddDays(0);
}
注:
下週:
date.AddDays(7);
前一週:
date.AddDays(-7);
,但我想從聊天室得到週末)選擇月份的日曆控制,然後我想將它們保存在列表中 – Wasif
// Get weekend days of visible moth
public List<DateTime> getWeekEndDays(DateTime dt)
{
List<DateTime> result = new List<DateTime>();
int month = dt.Month;
dt = dt.AddDays(-dt.Day + 1);//Sets dt to first day of month
//Sets dt to the first week-end day of the month;
if (dt.DayOfWeek != DayOfWeek.Sunday)
while (dt.DayOfWeek != DayOfWeek.Saturday)
dt = dt.AddDays(1);
//Adds the week-end day and stops when next month is reached.
while (dt.Month == month)
{
result.Add(dt);
dt = dt.AddDays(dt.DayOfWeek == DayOfWeek.Saturday ? 1 : 6);
}
return result;
}
在22秒內擊敗我! –
感謝您的回覆,但我需要從asp.net日曆控制中獲取週末 – Wasif
@Wasif您是否試圖選擇日曆控件上的週末? –