按周数列出全年的上市日期

本文关键字:日期 周数列 数列 | 更新日期: 2023-09-27 18:06:37

嗨,我想按周数列出全年的日期,这样输出就像

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)添加第二个范围,以便它按周数列出全年的日期。由于

按周数列出全年的上市日期

试试这个,它可以帮助你获得日期和星期号列表

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);

你必须根据你的需要定制你的输出