By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,594 Members | 3,459 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,594 IT Pros & Developers. It's quick & easy.

Calculate date from week number

P: n/a
Hi
Im using GregorianCalendar to find out the current years week numbers.
When the user chooses a week number in a dropdown i want to show that week
in a table with the corresponding dates.

For example : the user choses week43 (this week)
so somehow i must calculate what date is startdate that week (monday 18th).
How do i do this with c# ?

all i know is that its-
year: 2004
and week: 43

Ive been thinking about doing it the ugly way .. by taking weeknumber*7 to
get the day of year ... but that wouldnt be accurate since not all weeks have
7 days.

Then I thought about making a loop that goes thru all the days of the year
checking what week that date is in and if its 43 then jump out and return the
date.
But that would be very ugly ... and take unnecessary resources imo.
the Gregorian code is as follows :

GregorianCalendar gCalendar = new GregorianCalendar();
int YearNumber = gCalendar.GetYear(DateTime.Now);
string strMaxDate = YearNumber.ToString() + "-12-31";

int MaxWeekNumber = gCalendar.GetWeekOfYear(Convert.ToDateTime(strMaxD atum),
CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

for(int r = 1; r < MaxWeekNumber+1; r++)
{
selectWeek.Items.Add("Week " + r.ToString());
}
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Rustan wrote:
Hi
Im using GregorianCalendar to find out the current years week numbers.
When the user chooses a week number in a dropdown i want to show that
week in a table with the corresponding dates.

For example : the user choses week43 (this week)
so somehow i must calculate what date is startdate that week (monday
18th). How do i do this with c# ?

all i know is that its-
year: 2004
and week: 43

Ive been thinking about doing it the ugly way .. by taking
weeknumber*7 to get the day of year ... but that wouldnt be accurate
since not all weeks have 7 days.
is that true?

Then I thought about making a loop that goes thru all the days of the
year checking what week that date is in and if its 43 then jump out
and return the date.
But that would be very ugly ... and take unnecessary resources imo.


What you could do:
- check day-of-week and weeknumber for jan 1st of that year
- note: might be in last week of previous year!
- from there calculate startday of week 1
- then add the required number of days ( (weeknum-1)*7), using AddDays()

Hans Kesting
Nov 16 '05 #2

P: n/a
Hi Rustan,

You might be looking for the AddWeeks method
http://msdn.microsoft.com/library/de...weekstopic.asp

This might work:
Initialize a new date time for the 1st of Jan for the required year and then
call the AddWeeks method.

HTH,
Rakesh Rajan

"Rustan" wrote:
Hi
Im using GregorianCalendar to find out the current years week numbers.
When the user chooses a week number in a dropdown i want to show that week
in a table with the corresponding dates.

For example : the user choses week43 (this week)
so somehow i must calculate what date is startdate that week (monday 18th).
How do i do this with c# ?

all i know is that its-
year: 2004
and week: 43

Ive been thinking about doing it the ugly way .. by taking weeknumber*7 to
get the day of year ... but that wouldnt be accurate since not all weeks have
7 days.

Then I thought about making a loop that goes thru all the days of the year
checking what week that date is in and if its 43 then jump out and return the
date.
But that would be very ugly ... and take unnecessary resources imo.
the Gregorian code is as follows :

GregorianCalendar gCalendar = new GregorianCalendar();
int YearNumber = gCalendar.GetYear(DateTime.Now);
string strMaxDate = YearNumber.ToString() + "-12-31";

int MaxWeekNumber = gCalendar.GetWeekOfYear(Convert.ToDateTime(strMaxD atum),
CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

for(int r = 1; r < MaxWeekNumber+1; r++)
{
selectWeek.Items.Add("Week " + r.ToString());
}

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.