3

If I type

DateString[{2011, 2, 29, 0, 0, 0}, {"DayName"}]

It gives "Tuesday".

And also,

DateString[{2011, 2, 29, 0, 0, 0}, {"DayName"}]

DateString[{2011, 3, 1, 0, 0, 0}, {"DayName"}]

Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
Qiang Li
  • 10,593
  • 21
  • 77
  • 148

2 Answers2

6

This looks to me like correct behaviour. The documentation for DateString says: "Values of m, d, h, m, s outside their normal ranges are appropriately reduced." which is just what's happened: there isn't really a 29th of February this year, but if there were it would be the same day that's actually the 1st of March, which is indeed a Tuesday.

Gareth McCaughan
  • 19,888
  • 1
  • 41
  • 62
  • 1
    I would rather to have some exception thrown, rather than giving this misleading `"Tuesday"` answer! How can I write my own wrapper version of DateString to achieve this? Many thanks. – Qiang Li Mar 15 '11 at 02:09
  • 2
    The documentation also gives the [example](http://reference.wolfram.com/mathematica/ref/DateString.html#47250627) `DateString[{2006, 2, 31}]` => `Fri 3 Mar 2006 00:00:00`. – WReach Mar 15 '11 at 02:19
  • 1
    You can feed a date list into the `DateList` function, which "converts a date list to standard normalized form". So a date list is well-formed iff it's unaltered by doing this. You could throw an exception if that isn't the case. – Gareth McCaughan Mar 15 '11 at 03:00
  • @Qiang Please note that this behavior allows for a VERY convenient way to do some date arithmetic (adding or subtracting days, months, years ...) – Dr. belisarius Mar 15 '11 at 17:58
6
Needs["Calendar`"];
myDay[x_List] := DateString[x, {"DayName"}] /; DateQ[x]  

myDay[{2000, 1, 1}]
->"Saturday"

myDay[{2000, 13, 13}]
->myDay[{2000, 13, 13}]  

Of course you may throw a message (or Abort[], or whatever) if you want to :

Needs["Calendar`"];
Clear@myDay;
myDay[x_] /; If[DateQ[x], True, Message[myDay::nodate, x]; False] := 
                                                       DateString[x, {"DayName"}]
myDay::nodate = "The argument `1` is not a valid date.";
Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190