6
$\begingroup$

I have dates given for a TimeLinePlot. These dates are in {year,week} format.

DateObject does not take this with DateObject[{2014,26},DateFormat->{"Year","Week"}]. DateFormat should handle DateString format. There the week is given as option.

Does anyone have an idea how to make it work?

$\endgroup$
4
$\begingroup$

You could use DateList to work out the date. For example, (naïvely assuming week 1 begins on January 1st).

{year, week} = {2014, 26};

date = DateList[{year, 1, (week - 1)*7 + 1}]

{2014, 6, 25, 0, 0, 0.}

For example:

dates = {{2014, 2}, {2014, 8}, {2014, 16}, {2014, 26}};
values = {4, 6, 3, 8};
newdates = DateList[{#1, 1, (#2 - 1)*7 + 1}] & @@@ dates;
DateListPlot[Transpose[{newdates, values}]]

enter image description here

Edit

Using ISO weeks and adjusting accordingly. (ISO week is taken to be "the week with the year's first Thursday in it ", so week 1 of 2014 begins on December 30th 2013.)

enter image description here

isodate[{year_, week_}] := Module[{isoAdjust },
  isoAdjust = Cases[Map[{#, DateString[{year, 1, #},
          "DayNameShort"]} &, Range[7]], {_, "Thu"}][[1, 1]] - 3;
  DateList[{year, 1, (week - 1)*7 + isoAdjust}]]

isodate[{2014, 1}]

{2013, 12, 30, 0, 0, 0.}

isodate[{2014, 26}]

{2014, 6, 23, 0, 0, 0.}

newdates = isodate /@ dates;
DateListPlot[Transpose[{newdates, values}]]   
$\endgroup$
  • $\begingroup$ Just one comment for sake of clarity. The US does not ISO weeks. They use first Saturday to start the week numbering. Thank you for your work. $\endgroup$ – Eisbär Jan 27 '16 at 20:16
5
$\begingroup$

Note This answer exposed a bug in DateList's handling of the "Week" (or "WeekShort") element for week one when it begins in the previous year. This was spotted by @ChrisDegnen (see the comments) and has been acknowledged by Wolfram who suggested the workaround.


I don't believe DateObject has a concept of a week as a period of time in the same way that it seems to for a year, month or day, so I think the best you can do is a DateObject for the first day of the week.

And since the DateFormat option for DateObject is the display format rather than an interpretation format we use DateList to do the conversion...

fromYearWeek[{y_Integer, w_Integer}] := 
 DateObject[
  DatePlus[DateList[{ToString[y] <> "-" <> ToString[w], {"Year", 
      "Week"}}], 1], DateFormat -> {"ISOWeekDate"}]
fromYearWeek[{y_Integer, 1}] := 
 DateObject[
  DatePlus[DateList[{ToString[y] <> "-2", {"Year", 
      "Week"}}], -6], DateFormat -> {"ISOWeekDate"}]

Note that I have added a day to convert to ISO weeks which begin on a Monday and changed the DateObject display format to indicate the week number.

It includes a special case for week one - calculating the date list for week two and subtracting 6 days.

$\endgroup$
  • $\begingroup$ Interesting about ISO weeks but the code is wrong: DateList[{"2014 1", {"Year", "WeekShort"}}] produces {2014, 12, 29, 0, 0, 0.} - miles out. Consequently fromYearWeek[{2014, 1}] produces 2015-W01-2 GMT. $\endgroup$ – Chris Degnen Jan 27 '16 at 15:00
  • $\begingroup$ from YearWeek[{2014, 2}] gives 2014-W02-2 GMT which is okay. But I did not find the reason yet. Spezifiying that a DatePlus[...,{1,"Day"}] should add a day does not help. $\endgroup$ – Eisbär Jan 27 '16 at 15:06
  • $\begingroup$ @ChrisDegnen my non-extensive testing did not spot that slight problem! I guess this is something to do with week 1 containing the first Thursday so I guess your custom solution is better. $\endgroup$ – MikeLimaOscar Jan 27 '16 at 15:39
  • 1
    $\begingroup$ It looks like a Mathematica bug. DateList[{"2014 1", {"Year", "WeekShort"}}] is stepping back but not decrementing the year, so it finds Monday in December but for 2014, not 2013. Same problem happens for DateList[{"2013 1", {"Year", "WeekShort"}}]. However DateList[{"2015 1", {"Year", "WeekShort"}}] is correct : {2014, 12, 29, 0, 0, 0.} - perhaps because Jan 1st 2015 is a Thursday. $\endgroup$ – Chris Degnen Jan 27 '16 at 16:00
  • $\begingroup$ @ChrisDegnen Agreed. I'll report this to Wolfram support. $\endgroup$ – MikeLimaOscar Jan 27 '16 at 16:45

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.