嗨我想列出全年的日期编号,以便输出就像
Date Week#
21-03-2015 3
22-03-2015 3
23-03-2015 3
24-03-2015 3
25-03-2015 3
26-03-2015 3
27-03-2015 3
....
....
.....
21-12-2015 53
22-12-2015 53
23-12-2015 53
24-12-2015 53
25-12-2015 53
26-12-2015 53
27-12-2015 53
以下代码只会
public static DateTime[] WeekDays(int Year, int WeekNumber)
{
DateTime start = new DateTime(Year, 1, 1).AddDays(7 * WeekNumber);
start = start.AddDays(-((int)start.DayOfWeek));
return Enumerable.Range(0, 7).Select(num => start.AddDays(num)).ToArray();
}
请告诉我如何为(0,54)添加第二个范围,以便按周编号列出全年的日期。 谢谢
答案 0 :(得分:0)
试试这个,它可以帮助您获取日期和周数列表
public class DateAndWeek
{
public DateTime Date { get; set; }
public int WeekNumber { get; set; }
}
private List<DateAndWeek> GetListOfDateAndWeek(int startYear, int startMonth, int startDay)
{
List<DateAndWeek> dateTimes = new List<DateAndWeek>();
var week = 0;
var dateTimeStart = new DateTime(startYear, startMonth, startDay);
for (int i = 0; i < 365; i++)
{
var dateTime = dateTimeStart.AddDays(i);
if (dateTime.DayOfWeek == DayOfWeek.Monday)
{
week++;
}
dateTimes.Add(new DateAndWeek { Date = dateTime, WeekNumber = week });
}
return dateTimes;
}
以这种方式使用
var list = GetListOfDateAndWeek(2015, 1, 1);
您必须根据需要自定义输出。