MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Numbering weeks for the years of this century. An easy problem?

  • To: mathgroup at
  • Subject: [mg89138] Re: Numbering weeks for the years of this century. An easy problem?
  • From: "Hans Michel" <hmichel at>
  • Date: Mon, 26 May 2008 06:25:17 -0400 (EDT)
  • References: <g18hl1$kcr$>


A google search for ISO Week and Excel would yield lot of choices.

For Excel solution not using the WEEKNUM which they know is wrong function 

Now if you take the following VB solution from that page:

Public Function IsoWeekNum(d1 As Date) As Integer
' Provided by Daniel Maher.
   Dim d2 As Long
   d2 = DateSerial(Year(d1 - Weekday(d1 - 1) + 4), 1, 3)
   IsoWeekNum = Int((d1 - d2 + Weekday(d2) + 5) / 7)
End Function

Convert to Mathematica

ISOWeek[x_] := Module[{d2},
  d2 = DateList[{ToExpression[
        x, {(-(Flatten[
               Position[{"Sunday", "Monday", "Tuesday", "Wednesday",
                 "Thursday", "Friday", "Saturday"},
                DateString[DatePlus[x, {-1, "Day"}], {"DayName"}],
                1]])[[1]] + 4), "Day"}], "Year"]], 1, 3, 0, 0, 0}];
  IntegerPart[(DateDifference[d2, x] +
        Position[{"Sunday", "Monday", "Tuesday", "Wednesday",
          "Thursday", "Friday", "Saturday"},
         DateString[d2, {"DayName"}], 1]][[1]] + 5)/7]
Use as follows
ISOWeek[DateList[{2000, 1, 1, 0, 0, 0}]]

Returns same value as VB solution, does not make it correct!

On this news group there have been solutions to get the equivalent of 
excel's DateSerial. This vb solution has many dependencies on how Microsoft 
calculates and serializes dates.

I have not tested the VB nor the Mathematica code translation enough to say 
this is a solution but leave the rest to others. There is plenty of room for 
improvement. Because translation from VB "Sunday" is first day of the week; 
but in ISO "Monday" is first day.

Note other links
"Guillermo Sanchez" <guillermo.sanchez at> wrote in message 
news:g18hl1$kcr$1 at
> According to ISO 8601:2004(E), parr. 2.2.10 the  a calendar week
> number is defined as ordinal number which identifies a calendar week
> within its calendar year according to the rule that the first calendar
> week of a year is that one which includes the first Thursday of that
> year and that the last calendar week of a calendar year is the week
> immediately preceding the first calendar week of the next calendar year.
> Could any body build a function for numbering the week for the years
> this century?
> A think it is not an easy problem (for instance teh solution given by
> Excel sometime is wrong
> A solution for period 2000-2027 is also wellcome. In this period the
> 53-week years are 2004, 2009, 2015, 2020, 2026;
> Guillermo

  • Prev by Date: Re: Re: No Show
  • Next by Date: Re: Please Help with Sums with the same StandardForm but different
  • Previous by thread: Numbering weeks for the years of this century. An easy problem?
  • Next by thread: Caution! Reuse of variables within Manipulate