如何获取给定月份的所有日期以及前几天和次日的列表,以填写一周, 并选择周末和节假日。
我需要返回带有以下内容的XML文件:
[XmlAttribute("month")]
[Display(Name = "Month", Description = "")]
public int Month { get; set; }
/// <summary>
/// Day
/// </summary>
[XmlAttribute("weekday")]
[Display(Name = "Weekday", Description = "")]
public int Weekay { get; set; }
/// <summary>
/// Weekend/ Holiday Day
/// </summary>
[XmlAttribute("weekendday")]
[Display(Name = "Weekendday", Description = "")]
public int Weekendday { get; set; }
就像Windows中的日历一样。 我尝试了这个,但只得到了当前的月份
var days = Enumerable.Range(1, DateTime.DaysInMonth(year, month)) // Days: 1, 2 ... 31 etc.
.Select(day => new CalendarItem(year, month, day))
.ToList();
答案 0 :(得分:0)
使用此DateTime扩展名:
static class DateExtensions
{
public static IEnumerable<DateTime> GetRange(this DateTime source, int days)
{
for (var current = 0; current < days; ++current)
{
yield return source.AddDays(current);
};
}
public static DateTime NextDayOfWeek(this DateTime start, DayOfWeek dayOfWeek)
{
while (start.DayOfWeek != dayOfWeek)
start = start.AddDays(-1);
return start;
}
}
在我的课堂上,我用这个:
var numberOfDays = 42;
DateTime startDate = new DateTime(year, month, 1).NextDayOfWeek(DayOfWeek.Monday);
var dates = startDate.GetRange(numberOfDays)
.Select(date => new CalendarDaysItem(date.Month, date.Day))
.ToList();
定义const以获取数字天; 获取星期几作为开始日期的星期几;