[VBScript] week number
Solved
Dinheru
Posted messages
1019
Registration date
Status
Membre
Last intervention
-
babou -
babou -
Hello everyone!! It's me again!! ^^
This time I would like to know if there is a VBScript function that returns the current week number.
I've already found for:
- the day -->
- the month -->
- the year -->
Is there a function like this:
Thank you in advance!!
--
Life is like chocolate, sometimes hard and bitter,
sometimes hot and sweet.
This time I would like to know if there is a VBScript function that returns the current week number.
I've already found for:
- the day -->
day = day(now)
- the month -->
month = month(now)
- the year -->
year = year(now)
Is there a function like this:
week=week(now)(But that doesn't work!! )
Thank you in advance!!
--
Life is like chocolate, sometimes hard and bitter,
sometimes hot and sweet.
8 réponses
Simpler than that, we can use the format version present in VBA, which can conform to the ISO standard.
Example:
In this case, Format returns 29, which is the correct answer.
The vbMonday argument defines the first day of the week (according to the standard, Monday, but Sunday by default for VBA if the argument is omitted) and vbFirstFourDays defines that week #1 is the first week containing at least 4 days (i.e., the week containing the first Thursday of the year, it is equivalent; if the argument is omitted, VBA considers it to be the week of January 1st, which is not always accurate)
Of course, we can use variables to define the date, using the CDate function:
We can also only use the last 2 digits of the year, VBA considers that the year is between 1930 and 2029
Note aside, the Format function allows you to format a date (among other things), it is the argument "ww" that makes it return the week number. By replacing it for example with "dddd d mmmm yyyy", the function returns in our example "Friday 32 July 2010"
Example:
Dim datetest, week datetest = #23/7/2010# week = Format(datetest, "ww", vbMonday, vbFirstFourDays)
In this case, Format returns 29, which is the correct answer.
The vbMonday argument defines the first day of the week (according to the standard, Monday, but Sunday by default for VBA if the argument is omitted) and vbFirstFourDays defines that week #1 is the first week containing at least 4 days (i.e., the week containing the first Thursday of the year, it is equivalent; if the argument is omitted, VBA considers it to be the week of January 1st, which is not always accurate)
Of course, we can use variables to define the date, using the CDate function:
Dim day, month, year Dim datetest, week day = 23 month = 7 year = 2010 datetest = CDate(day & "/" & month & "/" & year) week = Format(datetest, "ww", vbMonday, vbFirstFourDays)
We can also only use the last 2 digits of the year, VBA considers that the year is between 1930 and 2029
Note aside, the Format function allows you to format a date (among other things), it is the argument "ww" that makes it return the week number. By replacing it for example with "dddd d mmmm yyyy", the function returns in our example "Friday 32 July 2010"
week = Format(DD, "ww", vbMonday, vbFirstJan1)