473,486 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Getting the weeknumber

In Denmark the 1/1/2009 belongs to week 53 of year 2008.

The code below, as I believe should apply to Danish settings, returns
1, which is wrong.

Am I having a wrong approach?
'--code begin
Dim iWeek As Integer
Dim gc As New System.Globalization.GregorianCalendar

gc.CalendarType = Globalization.GregorianCalendarTypes.Localized
iWeek = gc.GetWeekOfYear(#1/1/2009#,
Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)

MsgBox(iWeek)
'-- code end --

I have a function for SQL-server that works perfectly, but is it
really necessary for such an ugly solution?

DECLARE @ISOweek varchar(10)
DECLARE @DATE datetime
DECLARE @s varchar(10)
DECLARE @Year int

SET @DATE='01-01-2009'
SET @ISOweek= CONVERT(varchar,DATEPART(wk,@DATE)+1
-DATEPART(wk,CAST(DATEPART(yy,@DATE) as CHAR(4))+'0104'))
--Special cases: Jan 1-3 may belong to the previous year
IF (@ISOweek='0')
SET
@ISOweek=CONVERT(varchar,dbo.ISOweek(CAST(DATEPART (yy,@DATE)-1
AS CHAR(4))+'12'+ CAST(24+DATEPART(DAY,@DATE) AS
CHAR(2)))+1)+','+CONVERT(varchar,YEAR(@DATE)-1)
--Special case: Dec 29-31 may belong to the next year
IF ((DATEPART(mm,@DATE)=12) AND
((DATEPART(dd,@DATE)-DATEPART(dw,@DATE))>= 28))
SET @ISOweek='1'
PRINT (@ISOweek)
Regards /Snedker
--
http://www.dbconsult.dk
http://www.vinthervej2.dk [private]
May 16 '07 #1
4 3976
1 January 2009 is a Thursday and there are 4 days of January 2009 before the
first Monday in January 2009, therefore the week number is 1 which is
correct.

Maybe you need to tell us what the 'Denmark specific' requirement is as it
appears that the 'Denmark specific' requirement does not comply with ISO
8601.
"Morten Snedker" <morten_spammenot_ATdbconsult.dkwrote in message
news:do********************************@4ax.com...
In Denmark the 1/1/2009 belongs to week 53 of year 2008.

The code below, as I believe should apply to Danish settings, returns
1, which is wrong.

Am I having a wrong approach?
'--code begin
Dim iWeek As Integer
Dim gc As New System.Globalization.GregorianCalendar

gc.CalendarType = Globalization.GregorianCalendarTypes.Localized
iWeek = gc.GetWeekOfYear(#1/1/2009#,
Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)

MsgBox(iWeek)
'-- code end --

I have a function for SQL-server that works perfectly, but is it
really necessary for such an ugly solution?

DECLARE @ISOweek varchar(10)
DECLARE @DATE datetime
DECLARE @s varchar(10)
DECLARE @Year int

SET @DATE='01-01-2009'
SET @ISOweek= CONVERT(varchar,DATEPART(wk,@DATE)+1
-DATEPART(wk,CAST(DATEPART(yy,@DATE) as CHAR(4))+'0104'))
--Special cases: Jan 1-3 may belong to the previous year
IF (@ISOweek='0')
SET
@ISOweek=CONVERT(varchar,dbo.ISOweek(CAST(DATEPART (yy,@DATE)-1
AS CHAR(4))+'12'+ CAST(24+DATEPART(DAY,@DATE) AS
CHAR(2)))+1)+','+CONVERT(varchar,YEAR(@DATE)-1)
--Special case: Dec 29-31 may belong to the next year
IF ((DATEPART(mm,@DATE)=12) AND
((DATEPART(dd,@DATE)-DATEPART(dw,@DATE))>= 28))
SET @ISOweek='1'
PRINT (@ISOweek)
Regards /Snedker
--
http://www.dbconsult.dk
http://www.vinthervej2.dk [private]
May 16 '07 #2
On Wed, 16 May 2007 20:52:38 +1200, "Stephany Young" <noone@localhost>
wrote:
>1 January 2009 is a Thursday and there are 4 days of January 2009 before the
first Monday in January 2009, therefore the week number is 1 which is
correct.

Maybe you need to tell us what the 'Denmark specific' requirement is as it
appears that the 'Denmark specific' requirement does not comply with ISO
8601.
You're right and I'm wrong. The code works fine, I just gave a
bad/wrong example.

I just have to put up a set of rules so I can figute out which year
the week belongs to.

With my CultureInfo 1/1/2010 is week 53, but week 53 belongs to 2008.

Regards /Snedker
--
http://www.dbconsult.dk
http://www.vinthervej2.dk [private]
May 16 '07 #3
On Wed, 16 May 2007 11:06:00 +0200, Morten Snedker
<morten_spammenot_ATdbconsult.dkwrote:

>With my CultureInfo 1/1/2010 is week 53, but week 53 belongs to 2008.
Jeez... 1/1/2010 is week 53, but belongs to 2009, of course, not 2008.

I meant what i didn't say. ;-)

Regards /Snedker
--
http://www.dbconsult.dk
http://www.vinthervej2.dk [private]
May 16 '07 #4
Wrong again!

1 January 2010 is in 53 of 2009 (not 2008).

The rule is simple and hold true for any year.

Public Structure ISO8601WeekYear

Public Week As Integer
Public Year As Integer

End Structure

Public Function GetISO8601WeekYear(ByVal date As DateTime) As
ISO8601WeekYear

Dim _iso8601weekyear As ISO8601WeekYear

Dim gc As New GregorianCalendar(GregorianCalendarTypes.Localized )

_iso8601weekyear.Week = gc.GetWeekOfYear(date,
CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)

_iso8601weekyear.Year = date.Year

If _iso8601weekyear.Week 1 AndAlso date.Month = 1 AndAlso date.Day < 5
Then _iso8601weekyear.Year -=1

Return iso8601weekyear

End Function
"Morten Snedker" <morten_spammenot_ATdbconsult.dkwrote in message
news:j3********************************@4ax.com...
On Wed, 16 May 2007 20:52:38 +1200, "Stephany Young" <noone@localhost>
wrote:
>>1 January 2009 is a Thursday and there are 4 days of January 2009 before
the
first Monday in January 2009, therefore the week number is 1 which is
correct.

Maybe you need to tell us what the 'Denmark specific' requirement is as it
appears that the 'Denmark specific' requirement does not comply with ISO
8601.

You're right and I'm wrong. The code works fine, I just gave a
bad/wrong example.

I just have to put up a set of rules so I can figute out which year
the week belongs to.

With my CultureInfo 1/1/2010 is week 53, but week 53 belongs to 2008.

Regards /Snedker
--
http://www.dbconsult.dk
http://www.vinthervej2.dk [private]
May 16 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1341
by: Vanessa | last post by:
Does anyone know whether when I select a weeknumber of a certain year, eg. 2004, the date from first day to the last day of the week will be known. Regards Vanessa
5
8509
by: DD | last post by:
Hi there, I am working on a sql-query and I need to know the weeknumber of a given date. Could anyone please help me with this. Thanks in advance, Arjen
3
1871
by: dik mus | last post by:
Hi, I connect to a database that only stores the year and weeknumber. But i need the date of the corresponding monday. This could be more difficult as one expect because the weekumbers might...
2
1089
by: | last post by:
Hi!! know someone how to get a week span with week number and year? something like hi: public static DateTime GetWeekSpan(int weekNumber, int year) { DateTime weekStartDate = new...
10
5248
by: bjaj | last post by:
Hi I have made this msgbox to display the current Week number, but it shows the wrong weekno. Can anyone tell me why ? msg = "WeekNumber: " & DatePart("WW", Date) MsgBox msg It shows...
4
4692
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...
4
1899
by: Longkhi | last post by:
Hi Everybody. I'm developing a dentist program, which contains a MonthCalender. I would like to select a specific weeknumber, when I select a weeknumber in the MC. I can show the weeknumbers by...
1
2590
by: peetersb | last post by:
Hi, I want ot make functions like this: int getFirtDayOfWeek(int weeknumber, int year); int getLastDayOfWeek(int weeknumber, int year); Firts I calculate the count of weeks like this: ...
0
7126
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
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6842
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
7330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4865
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...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.