set a date value

G

Guoqi Zheng

Dear sir,

We are on the week of 27 in year 2004. So this week is "2004-27", last week
is "2004-26",

My question is how can I get the first sunday of week "2004-26", how can I
change string "2004-26" into a DateTime variable with the value of first
sunday of week 2004-26???

--
Kind regards

Guoqi Zheng
guoqi AT meetholland dot com
Http://www.meetholland.com
 
E

EijiTek

The easiest way I could think of would be to check what the first day of the
year is (1/1/2004) through the DayOfWeek property of the DateTime class and
calculate the offset between it and the previous Sunday.

For 2004, calling DateTime.DayOfWeek for 1/1/2004 would return Thursday.
Therefore, the first week of 2004 (by week numbers) started on Sunday,
12/28/2003. To determine the Sunday starting any given week during 2004 you
could simply calculate it via:

public DateTime GetWeekStart(int WeekNumber)
{
return FirstSundayOfYear.AddDays(7*(WeekNumber-1));
}

The rationale for (WeekNumber-1) is that the FirstSundayOfYear would already
be set to 12/28/2003 so if you were looking for week 1 then you would want
to add 0 days as opposed to 7.

You indicated that you wanted to convert a string (2004-26) into a DateTime
for the first sunday of each week. What I would probably do in this
scenario since obviously no direct conversion exists would be to define a
WeekDictionary class derived from System.Collections.DictionaryBase and set
the key of the key to the desired string and the value to the appropriate
DateTime value.

Hope this helps!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top