473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Calculate date from week number

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
2 34311
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: CrystalDBA | last post by:
I am using SQL Server 2000. I need to query my database for all the contracts that came in during a certain time frame (user is prompted for reportingperiodid). Table - Periods Fields -...
1
by: David | last post by:
Can someone help me with this. I need to calculate the week ending date of the first week of the year based upon a year provided by the user. Is there a simpler way other than writing my own...
3
by: | last post by:
Hello, I am hoping someone else has thought about a date time calculation i need to perform. I would like to be able to calculate the number of "working minutes" between 2 dates, given my...
9
by: griemer | last post by:
Hi every one, Is there a way to calculate the timestamp of 00:00:00 last Sunday. For me, this is the begin of the current week.. Or, how old is this week, in seconds Any ideas? Regards,
5
by: Michael R. Copeland | last post by:
I need to determine a given date's week number, but my searches of google only confuse and frustrate me: there are many apparent formulas (which are pretty complex) and they all seem to require...
4
by: khicon73 | last post by:
Hello All, I would like to calculate weeknumber and period from first day of the week (sunday) and last day of the week (saturday) and week number falls from 1 to 52 only. If week number >= 53 then...
0
by: SuzK | last post by:
I am trying to calculate in VBA in Access 2002 a moving average and update a table with the calculations. Fields in my WeeklyData table are Week Ending (date) ItemNbr (double) Sales Dollars...
4
by: Vince | last post by:
Given a week Number, how do I calculate the date that for the Monday of that week?
15
by: student4lifer | last post by:
Hello, I have 2 time fields dynamically generated in format "m/d/y H:m". Could someone show me a good function to calculate the time interval difference in minutes? I played with strtotime() but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.