开发者

How to find the dates of a calendar week?

How to get the date of a specific week in a specific year?

For example: I have the data week开发者_StackOverflow 30, year 2009 - how I can get the date of the week?

Language is C#


A sample for code that does it:

int workWeek = 30;
int year = 2009;

var firstDayOfYear = new DateTime(year, 1, 1);
var firstDayOfWorkWeek = firstDayOfYear.AddDays((workWeek - 1) * 7);

This code assumes that the work week start on the day of the 1/1/YYYY (meaning week can start from Wednesday for example).

If you want to normalize it to Sunday:

firstDayOfWorkWeek = 
               firstDayOfWorkWeek.AddDays(-(int) firstDayOfWorkWeek.DayOfWeek);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜