שתף באמצעות


Weeknumber

Question

Tuesday, March 21, 2006 11:50 AM

How do i get the weeknumber in Visual Basic 2005 Express edition ?

All replies (18)

Saturday, March 25, 2006 6:51 PM ✅Answered | 1 vote

For those who require it the Answer shown above can be modified to give the ISO compliant week number with the addition of a couple of extra parameters.

Dim WeekNumber As Integer = DatePart(DateInterval.WeekOfYear, Date.Today, _

FirstDayOfWeek.Monday, FirstWeekOfYear.FirstFourDays)

 


Saturday, November 1, 2008 7:32 PM ✅Answered

 This one will give you the weeknumber in the way your system is setup !

Dim WeekNumber as Integer = DatePart(DateInterval.WeekOfYear, Date.Now, FirstDayOfWeek.Monday, FirstWeekOfYear.System)


Tuesday, March 21, 2006 1:25 PM

Format(Now(), "ww")


Tuesday, March 21, 2006 1:34 PM

Well that doesn't work for me.

There's an interesting article about this here

http://www.thecodeproject.com/csharp/GregToISO.asp?msg=396665

The code is in C# but shouldn't be too difficult to convert


Tuesday, March 21, 2006 1:48 PM

You can work it out fairly easily..

I think my number of days in a leap year is wrong (i can never remember) but it think it works about close enough anyway

 

Private Sub WhatWeek

Dim DOY As Integer = Now.DayOfYear  ' Get the current day of year

Dim NOD As Integer  'number of days in a year

Dim NW As Integer = 52 'number of weeks in a year (might have to mess with this with leap years)

Dim TY As Integer = Now.Year 'current year. for leap year checking

Dim CW As Integer ' current week

If Date.IsLeapYear(TY) = True Then

NOD = 365

Else

NOD = 366

End If

CW = DOY / NOD * NW

MsgBox(Format(CW, "#"))

End Sub

 

Its probably the wrong way to do it but i think it works. If you run that code exactly it should give you an answer of twelve. My check of outlook tells me its week 12 :)


Tuesday, March 21, 2006 1:55 PM

Depends upon what you actually need. For all I know he could have been asking for the week number of the current month.

Generic questions tend to result in generic answers. ;-)


Tuesday, March 21, 2006 2:09 PM

It only gives me the value "ww" I think it only works in VB6 or so !

Sorry for my short question !

I ment, How to get the weeknumber of the year with the weeks starting on mondays.

Anyway, Thanks for your quick response !

Thomas !


Tuesday, March 21, 2006 3:14 PM

Yeah, actually I had forgotten that feature was broken.

I did convert the C# code from the link posted by Dave299 if anyone is interested:

Try

Dim dt As New DateTime

dt = DateTime.Today

Console.WriteLine("Gregorian Date : {0}", dt.ToString("d", System.Globalization.DateTimeFormatInfo.InvariantInfo))

Console.WriteLine("ISO Date : {0}", DateEx.DisplayISODate(dt))

Catch ex As Exception

Console.WriteLine("Error " + ControlChars.Lf + ControlChars.Lf + " {0}", ex.ToString())

End Try

Public Class DateEx

' Static Method to check Leap Year

Public Shared Function IsLeapYear(ByVal yyyy As Integer) As Boolean

If yyyy Mod 4 = 0 AndAlso yyyy Mod 100 <> 0 OrElse yyyy Mod 400 = 0 Then

Return True

Else

Return False

End If

End Function 'IsLeapYear

 

' Static Method to return ISO WeekNumber (1-53) for a given year

Public Shared Function ISOWeekNumber(ByVal dt As DateTime) As Integer

' Set Year

Dim yyyy As Integer = dt.Year

' Set Month

Dim mm As Integer = dt.Month

' Set Day

Dim dd As Integer = dt.Day

' Declare other required variables

Dim DayOfYearNumber As Integer

Dim Jan1WeekDay As Integer

Dim WeekNumber As Integer = 0

Dim WeekDay As Integer

 

Dim i, j, k, l, m, n As Integer

Dim Mnth(12) As Integer

Mnth(0) = 0

Mnth(1) = 31

Mnth(2) = 59

Mnth(3) = 90

Mnth(4) = 120

Mnth(5) = 151

Mnth(6) = 181

Mnth(7) = 212

Mnth(8) = 243

Mnth(9) = 273

Mnth(10) = 304

Mnth(11) = 334

Dim YearNumber As Integer

' Set DayofYear Number for yyyy mm dd

DayOfYearNumber = dd + Mnth((mm - 1))

' Increase of Dayof Year Number by 1, if year is leapyear and month is february

If IsLeapYear(yyyy) = True AndAlso mm = 2 Then

DayOfYearNumber += 1

End If

' Find the Jan1WeekDay for year

i = (yyyy - 1) Mod 100

j = yyyy - 1 - i

k = i + i / 4

Jan1WeekDay = 1 + ((j / 100 Mod 4) * 5 + k) Mod 7

' Calcuate the WeekDay for the given date

l = DayOfYearNumber + (Jan1WeekDay - 1)

WeekDay = 1 + (l - 1) Mod 7

' Find if the date falls in YearNumber set WeekNumber to 52 or 53

If DayOfYearNumber <= 8 - Jan1WeekDay AndAlso Jan1WeekDay > 4 Then

YearNumber = yyyy - 1

If Jan1WeekDay = 5 OrElse (Jan1WeekDay = 6 AndAlso Jan1WeekDay > 4) Then

WeekNumber = 53

Else

WeekNumber = 52

End If

Else

YearNumber = yyyy

End If

' Set WeekNumber to 1 to 53 if date falls in YearNumber

If YearNumber = yyyy Then

If IsLeapYear(yyyy) = True Then

m = 366

Else

m = 365

End If

If m - DayOfYearNumber < 4 - WeekDay Then

YearNumber = yyyy + 1

WeekNumber = 1

End If

End If

If YearNumber = yyyy Then

n = DayOfYearNumber + (7 - WeekDay) + (Jan1WeekDay - 1)

WeekNumber = n / 7

If Jan1WeekDay > 4 Then

WeekNumber -= 1

End If

End If

Return WeekNumber

End Function 'ISOWeekNumber

' Static Method to Calculate WeekDay (Monday=1...Sunday=7)

Public Shared Function WeekDay(ByVal dt As DateTime) As Integer

' Set Year

Dim yyyy As Integer = dt.Year

' Set Month

Dim mm As Integer = dt.Month

' Set Day

Dim dd As Integer = dt.Day

' Declare other required variables

Dim DayOfYearNumber As Integer

Dim Jan1WeekDay As Integer

Dim WkDay As Integer

 

Dim i, j, k, l As Integer

Dim Mnth(12) As Integer

Mnth(0) = 0

Mnth(1) = 31

Mnth(2) = 59

Mnth(3) = 90

Mnth(4) = 120

Mnth(5) = 151

Mnth(6) = 181

Mnth(7) = 212

Mnth(8) = 243

Mnth(9) = 273

Mnth(10) = 304

Mnth(11) = 334

' Set DayofYear Number for yyyy mm dd

DayOfYearNumber = dd + Mnth((mm - 1))

' Increase of Dayof Year Number by 1, if year is leapyear and month is february

If IsLeapYear(yyyy) = True AndAlso mm = 2 Then

DayOfYearNumber += 1

End If

' Find the Jan1WeekDay for year

i = (yyyy - 1) Mod 100

j = yyyy - 1 - i

k = i + i / 4

Jan1WeekDay = 1 + ((j / 100 Mod 4) * 5 + k) Mod 7

' Calcuate the WeekDay for the given date

l = DayOfYearNumber + (Jan1WeekDay - 1)

WkDay = 1 + (l - 1) Mod 7

Return WkDay

End Function 'WeekDay

' Static Method to Display date in ISO Format (Year - WeekNumber - WeekDay)

Public Shared Function DisplayISODate(ByVal dt As DateTime) As String

Dim str As String

Dim year, WkDay, weeknumber As Integer

year = dt.Year

weeknumber = ISOWeekNumber(dt)

WkDay = WeekDay(dt)

str = year.ToString("d0") + "-" + weeknumber.ToString("d0") + "-" + WkDay.ToString("d0")

Return str

End Function 'DisplayISODate

End Class


Tuesday, March 21, 2006 3:17 PM

This code sample is from VB.Net 2003, so it may or may not work.

 

Dim CI As New System.Globalization.CultureInfo("en-US")

Dim Cal As System.Globalization.Calendar = CI.Calendar

MsgBox(Cal.GetWeekOfYear(Now, Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday))


Tuesday, March 21, 2006 3:19 PM | 2 votes

Dim WeekNumber As Integer = DatePart(DateInterval.WeekOfYear, Date.Today)

Tuesday, March 21, 2006 7:38 PM

That may be a very concise "solution" but unfortunately it doesn't give the right answer a lot of the time - like through the whole of 2005, 2010 and 2011.

It also shows some "weeks" only lasting for a day or so.  Week 1 in 2005 only lasted for one day for instance and week 2 started on January 2nd.  The final week (53) in 2004 only lasted for 6 days.  It also starts the week on Sunday and not Monday which is the usual start day.

It appears to start week 1 on January 1st and changes to week 2 on the following Sunday.  This is not what most people would expect when they talk about week number.

 


Tuesday, March 21, 2006 10:27 PM

 Dave299 wrote:

 It also starts the week on Sunday and not Monday which is the usual start day.

The visualbasic function does return the expected result if:

1. You use the correct start of the week which is Sunday (not Monday the work day)

2. You count a week as any 7 day period starting with Sunday that contains at least a single day


Tuesday, March 21, 2006 11:50 PM

 DMan1 wrote:
2. You count a week as any 7 day period starting with Sunday that contains at least a single day

Yeah right !!  Name a 7 day period that doesn't contain a single day.

If you look at the end of 2004 into 2005 your method produces the following result:

December 31st 2004      Week 53

January 1st 2005        Week 01

January 2nd 2005        Week 02

by any reckoning surely this is nonsense.

I think most people would expect week number to refer to the week number they see when they look in their diaries.  In every diary I have looked at this is calculated to the ISO 8601 standard

In this, week 1 is the first week of the year which contains a Thursday and it starts on the previous Monday even if that Monday was in the previous year.

Have a look at the link I posted above for more information.

 

Dave


Wednesday, March 22, 2006 1:25 AM

Dave,

First off, I'm not trying to be sarcastic nor am I going to get into any debate about this with you. I will just try and explain the reasoning behind the function results. I'm sorry that you seem to have a difference of opinion on this matter.  This year is a prime example of a week containing only a single day and that would be the 53 week of the year December 31,2006

Consider the following

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim D1 As Date = #1/1/2006#

Dim D2 As Date = #1/8/2006#

Dim D3 As Date = #12/31/2006#

Console.WriteLine(GetWeekNumber(D1))

Console.WriteLine(GetWeekNumber(D2))

Console.WriteLine(GetWeekNumber(D3))

End Sub

Private Function GetWeekNumber(ByVal D As Date) As Integer

Return DatePart(DateInterval.WeekOfYear, D)

End Function

The output of which is

1

2

53

The first day of this year is Sunday January 1st the first week of this year also starts on the same day and  the following Sunday starts week number 2 and so on until the last day of the year which only contains a single day (as in my #2) which is week 53!

If you change the years your results will not change because the first day of the year is always in the first week (logically of course) and 7 days from that is in week two and so on!

Using your example is also a great example of week one only containing a single day saturday january 1 2005. Sunday January 2nd, 2005 in fact starts the second week of the year.  The first week only had a single day included in it. (as in my #2)

I believe that our difference is a result of you thinking that the week of the year has to contain 7 complete days...Try and think of it this way, a week is a row of days in the year...

Lets look at Jan 05

Sun Mon Tue Wed Thu Fri Sat
1 WEEK1
2 3 4 5 6 7 8 WEEK2
9 10 11 12 13 14 15 WEEK3
16 17 18 19 20 21 22 WEEK4
23 24 25 26 27 28 29 WEEK5
30 31 WEEK6

Febuary 1st in in the sixth week of the year. Obviously some one else out there thinks that the weeks are calculated this way because the DatePart function returns what I expect to be the correct week number no matter your example.

by any reckoning surely this is nonsense.

Actually it makes perfect sense!


Wednesday, March 22, 2006 7:28 AM

Thank´s for all your answers !

and thank´s to you DMan1 !

It was the DatePart function i needed.

I also think that the way of defining the first week of the year differs somewhat in different countries !

But anyway the Datepart function does the jobb !

 

 


Wednesday, March 22, 2006 10:42 AM

 Thomas Malbeck wrote:

I also think that the way of defining the first week of the year differs somewhat in different countries !

which is precisely why ISO 8601 was introduced.

DMan - I wasn't particulary trying to be sarcastic, although reading it again maybe it came over that way.

The point is that there is a lot of confusion over this and so a standard was introduced to try and resolve it and that is what many people use these days.

Your code has been marked as an answer and that may lead people searching these forums to believe that it will produce a result to the ISO standard.  It doesn't.

 

Dave


Wednesday, March 22, 2006 1:03 PM

 

>>
Your code has been marked as an answer and that may lead people searching these forums to believe that it will produce a result to the ISO standard.  It doesn't.
<<

Yeah, but this happens quite frequently, which is why I tend to ignore anything marked as *the* answer. Plus I think forum moderators and Microsoft folks can mark their own responses as *the* answer. I'm guessing this is the case because some responses do not correctly answer the question (at least according to the OP).

In any event, I think if someone reads this complete thread they can choose which option works best for them. 


Monday, December 11, 2006 1:36 PM

Personnally, I think the above is THE ANSWER because it shows you how to use the datepart with your own parameters Thanks...