由网友(兮墨丶花易冷)分享简介:我要创建在C#中的功能是一个星期编号将返回我的日子,在那一周。I want to create a function in C# which for a week number will return me the days, in that week.例如,对于本周40号,我怎么能得到天:4/10,5/10,6...
我要创建在C#中的功能是一个星期编号将返回我的日子,在那一周。
I want to create a function in C# which for a week number will return me the days, in that week.
例如,对于本周40号,我怎么能得到天: 4/10,5/10,6/10,7/10,8/10,9/10,10/10。
For instance for week number 40, how can I get the days: 4/10, 5/10, 6/10, 7/10, 8/10, 9/10, 10/10.
感谢你在前进!
推荐答案
我想这应该做你想要的:
I think this should do what you want:
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,7)到(1,7)。
Although I treat Sunday as first day of week, if you want Monday as first day change range from (0,7) to (1,7).
如果要符合ISO标准,我觉得这应该工作:
If you want to conform the ISO standard, I think this should work:
public static DateTime[] WeekDays(int Year, int WeekNumber)
{
DateTime start = new DateTime(Year, 1, 4);
start = start.AddDays(-((int)start.DayOfWeek));
start = start.AddDays(7 * (WeekNumber - 1));
return Enumerable.Range(0, 7).Select(num => start.AddDays(num)).ToArray();
}
相关推荐
最新文章