7
$\begingroup$

When I input DayName[{2012,12,24}] I get Monday as output. What is the most effective way to convert Monday into 1, Tuesday into 2 etc.?

To do this, I can create a length-7 array to store all the day-name and then do some matching and return the matched position. But that approach is too clumsy. Is there any other lighter and faster way?

$\endgroup$
12
$\begingroup$

The most efficient way is not to use DayName but this:

DateValue[{2012, 12, 24}, "ISOWeekDay"]

Regarding Robert's comment

Well, that was a correct answer but not on the exact question. What if I don't have a date but just a weekday as e.g. Monday how do I now get the ISOWeekDay number. Without explicitly coding. Is there a a ready to use MMA function f with f[Monday] == 1

the shortest solution I found is

ISODay = System`DateObjectDump`$dowAssociation;

ISODay[Monday]
(* 1 *)
$\endgroup$
  • $\begingroup$ Well, that was a correct answer but not on the exact question. What if I don't have a date but just a weekday as e.g. Monday how do I now get the ISOWeekDay number. Without explicitly coding. Is there a a ready to use MMA function f with f[Monday] == 1 $\endgroup$ – Robert Nowak Jul 18 at 18:14
  • $\begingroup$ @RobertNowak See my edit above. $\endgroup$ – halirutan Jul 18 at 18:59
  • $\begingroup$ May I ask by which black spell incantation you discovered this dowAssociation? $\endgroup$ – Roman Jul 18 at 21:05
  • $\begingroup$ I was quite sure that at some point, WL's functions need a conversion from day-name to number, because how else would they implement the "ISOWeekDay". So I started digging with PrintDefinitions[DateValue] and followed the trail... $\endgroup$ – halirutan Jul 19 at 13:02
0
$\begingroup$
ISODay[day_] := Mod[With[{o = {1, 1, 0}}, DateDifference[
  DayPlus[o, 1, Monday],
  DayPlus[o, 1, day]
  ][[1]]], 7] + 1
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.