tags:

views:

30

answers:

1

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

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

Language is C#

+2  A: 

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);
Elisha
It don't work, I tried both. I tried with week 30, year 09. In my calender it's Monday. 20.07.2009. But in your algorithm its the 29.07.2009
Kovu
You're right, it should be '(workWeek - 1) * 7' instead of 'workWeek * 7 - 1'
Elisha
Im sorry but it don't work, its exactly 1 week to late
Kovu