获取日期范围由周数C# [英] Get Date Range by week number c#
问题描述
可能重复:
<一href="http://stackoverflow.com/questions/3854429/in-net-knowing-the-week-number-how-can-i-get-the-weekdays-date">In .NET,知道周数我怎么能得到工作日的日期?
您好,
我要跟你们大家的问题。 我如何获得一个给定的周数的日期范围。
例如: 如果我输入12周输出应该是:
21-03-2011
22-03-2011
23-03-2011
24-03-2011
25-03-2011
26-03-2011
27-03-2011
我真的希望你们能帮助我,我只是不能在任何地方找到awnser!
在此先感谢。
注意
我似乎已经错过了错误。目前的code已被更新为2012-01-30考虑到这个事实,我们现在得出
daysOffset
根据星期二它根据的Mikael史云逊的出现来解决这个问题。
这些ISO8601星期日期计算是有点靠不住的,但是这是你如何做到这一点:
日期时间jan1 =新的日期时间(YYYY,1,1);
INT daysOffset = DayOfWeek.Tuesday - jan1.DayOfWeek;
日期时间firstMonday = jan1.AddDays(daysOffset);
VAR CAL = CultureInfo.CurrentCulture.Calendar;
INT firstWeek = cal.GetWeekOfYear(jan1,CalendarWeekRule.FirstFourDayWeek,DayOfWeek.Monday);
VAR WEEKNUM = WW;
如果(firstWeek&其中; = 1)
{
WEEKNUM - = 1;
}
VAR的结果= firstMonday.AddDays(WEEKNUM * 7 + D - 1);
返回结果;
基本上计算出一个基准点,然后加入天,硬的东西,是因为有有53周,有时会发生在一月和第1周,有时可发生在12月的事实。你需要调整的,这是做的一个办法。
以上code计算日期过了一年(年)和周数(WW)和周(D)的日子。
Possible Duplicate:
In .net, knowing the week number how can I get the weekdays date?
Hello,
I've got a question for ya'll. How do i get the date range of a given week number.
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!
Thanks in advance.
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.
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;
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.
The above code calculates the date off a year (yyyy) and week number (ww) and day of week (d).
这篇关于获取日期范围由周数C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!