开发者

How to find the dates of a calendar week?

开发者 https://www.devze.com 2022-12-22 08:18 出处:网络
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?

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

精彩评论

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