Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting a date from a week number 1

Status
Not open for further replies.

soulpumpkin

Programmer
Feb 18, 2005
79
0
0
US
I've seen some similar question, but trying to get the week number from a date. I'm trying to get a date from a week number though, and I'm having a little trouble. The data coming in looks like this:
Code:
12-04 (this would be the 12th week of 2004)
13-04 (this would be the 13th week of 2004)
14-04 (this would be the 14th week of 2004)
etc...

Here is some of the code I am using:
Code:
Dim CulInfo As New System.Globalization.CultureInfo(cntry, False)
Dim arrDate As Array
strTempWeek = ""
arrDate = strTempWeek.Split("-")
arrDate(1) = CulInfo.DateTimeFormat.Calendar.ToFourDigitYear(arrDate(1))
strTempWeek = CulInfo.DateTimeFormat.Calendar.AddWeeks("1/1/" & arrDate(1), arrDate(0))

Results in strTempWeek from supplied dates above come out as:
Code:
3/25/2004 (should be 3/14/2004)
4/1/2004 (should be 3/21/2004)
4/8/2004 (should be 3/28/2004)

This is happening because the first day of that year is a Thursday, so it's going Thursday to Thurday.

How do get it calculate from the first week, not from the first day?

Soul Pumpkin
 
Code:
CulInfo.DateTimeFormat.CalendarWeekRule = CalendarWeekRule.FirstFourDayWeek
 
The first for that year is a three day week; will this still work in terms of counting correctly?

Soul Pumpkin
 
I've tried it, and it didn't work. I'm still getting the same date as before, starting on a Thursday, and it's for the week after when it should be.

Soul Pumpkin
 
It could also be affected by the FirstDayOfWeek property:
Code:
ci.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Monday
Using FirstFourDayWeek and DayOfWeek.Monday for 2004 results in week 12 starting on March 15.
 
SHelton thanks. That's closer, but I'm still off a little. Could it be the way I'm using the AddWeeks func?
Code:
CultureInfo.DateTimeFormat.CalendarWeekRule = CultureInfo.DateTimeFormat.CalendarWeekRule.FirstFourDayWeek
                CultureInfo.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Sunday
                arrDate(1) = CultureInfo.DateTimeFormat.Calendar.ToFourDigitYear(arrDate(1))
                strTempWeek = CultureInfo.DateTimeFormat.Calendar.AddWeeks("1/1/" & arrDate(1), arrDate(0) - 1)

Even after setting the FirstFourDayWeek and FirstDayOfWeek I was still starting on a Thursday, "3/18/2004". The date should be "3/14/2004", Sunday.

Is the AddWeeks func screwing it up by starting on the 1st?

Soul Pumpkin
 
Maybe. To avoid any potential culture clashes, use a properly declared DateTime structure in the AddWeeks method. Something like:
Code:
Dim dt As New DateTime(Convert.ToInt32(arrDate(1)), 1, 1, CultureInfo.Calendar)

strTempWeek = CultureInfo.DateTimeFormat.Calendar.AddWeeks(dt, arrDate(0) - 1)
(please excuse any syntax errors, this is from memory and I usually code in C#!)
 
Thanks SHelton. This helps. It is still overshooting my date by a few days, but I'm rolling back to the previous Sunday and it is working fine with my data.

Thank you for your time and input, you have helped me out alot.

Soul Pumpkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top