可能重复:
在 .net 中,了解周数如何获取工作日日期?
你好,
我有一个问题要问你们.如何获取给定周数的日期范围.
I've got a question for ya'll. How do i get the date range of a given week number.
例如:如果我进入第 12 周,输出应该是:
For example: If I enter week 12 the output should be:
21-03-2011
22-03-2011
23-03-2011
24-03-2011
25-03-2011
26-03-2011
27-03-2011
我真的希望你们能帮帮我,我只是在任何地方都找不到遮阳篷!
I really hope you guys can help me out, i just cant find the awnser anywhere!
提前致谢.
注意
我似乎错过了错误.当前代码已于 2012 年 1 月 30 日更新以说明这一事实,我们现在根据 Mikael Svenson 似乎解决了这个问题.
Note
I appear to have missed bug. The current code have been updated as of 2012-01-30 to account for this fact and we now derive the
daysOffset
based on Tuesday which according to Mikael Svenson appears to solve the problem.
这些 ISO8601 周日期计算有点不靠谱,但你是这样做的:
These ISO8601 week date calculations are a bit wonky, but this is how you do it:
DateTime jan1 = new DateTime(yyyy, 1, 1);
int daysOffset = DayOfWeek.Tuesday - jan1.DayOfWeek;
DateTime firstMonday = jan1.AddDays(daysOffset);
var cal = CultureInfo.CurrentCulture.Calendar;
int firstWeek = cal.GetWeekOfYear(jan1, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
var weekNum = ww;
if (firstWeek <= 1)
{
weekNum -= 1;
}
var result = firstMonday.AddDays(weekNum * 7 + d - 1);
return result;
基本上计算一个参考点,然后加上天,困难的事情与第 53 周有时可能发生在 1 月而第 1 周有时可能发生在 12 月这一事实有关.您需要对此进行调整,这是一种方法.
Basically calculate a reference point, then add days, the hard stuff has to do with the fact that week 53 can sometimes occur in January and week 1 can sometimes occur in December. You need to adjust for that and this is one way to do that.
上面的代码计算了一年的日期(yyyy)和周数(ww)和星期几(d).
The above code calculates the date off a year (yyyy) and week number (ww) and day of week (d).
这篇关于按周数获取日期范围c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!