9

I have the following string: "20110103224832494" it is in the following format: yyyyMMddHHmmssSSS. Where yyyy is the year, MM is the month, dd is the day, HH is the hour, mm is the minutes, ss is the seconds, SSS is the milliseconds.

I thought that the following would have worked:

DateList[{"20110103224832494",  
         {"Year", "Month", "Day", "Hour", "Minute", "Second", "Millisecond"}}
]

but returns:

DateString::str: String 20110103224832494 cannot be interpreted as a date in 
format {Year,Month,Day,Hour,Minute,Second,Millisecond}. >>

And if that worked, would it have been efficient?

Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
mmorris
  • 4,006
  • 3
  • 27
  • 29

2 Answers2

10

Use:

DateList[{"20110103224832494",  
   Riffle[{"Year",
           "Month", 
           "Day",   
           "Hour", 
           "Minute", 
           "Second", 
           "Millisecond"}, ""]}]
VirtualTroll
  • 3,077
  • 1
  • 30
  • 47
Brett Champion
  • 8,497
  • 1
  • 27
  • 44
  • I didn't know about this one. Nice. – Mr.Wizard Dec 01 '11 at 18:28
  • 2
    `DateList[{"20110103224832494", {"Year", "Month", "Day", "Hour", "Minute", "Second", "", "Millisecond"}}]` works too figured that out after posting question. – mmorris Dec 01 '11 at 18:32
  • 2
    Why does it require to have an empty string interspersed with the rest of the date/time specification? – rcollyer Dec 01 '11 at 18:45
  • @rcollyer see `DateList` docs > More Information > last line. – Mr.Wizard Dec 01 '11 at 18:54
  • @Mr.Wizard, I see that, but the question remains. Why isn't `{"Year", "", "Month"}` equivalent to `{"Year", "Month"}`? – rcollyer Dec 01 '11 at 19:03
  • @rcollyer, just above that: "... and can be separated by any non-alphanumeric characters." If what you are really asking: "Why wasn't this make equivalent" (which is the sort of question I like to ask) then I don't know; perhaps it introduces an ambiguity. – Mr.Wizard Dec 01 '11 at 19:06
  • 3
    @Mr.Wizard, actually that is what I'm asking. More specifically, I'm asking, why do I need separators when there are none? I can see why it would be frustrating. – rcollyer Dec 01 '11 at 19:10
  • ***This apparently does not work on version 7*** Instead I get: `{2011, 1, 12, 9, 23, 24.094}` – Mr.Wizard Dec 01 '11 at 20:10
  • 1
    How does mma know where to split the string? – DavidC Dec 01 '11 at 21:15
  • @David, standard field length, I suppose? – Mr.Wizard Dec 04 '11 at 10:58
  • @Mr.Wizard To me it was puzzling to intersperse empty strings between elements that were glommed together. But, why not? – DavidC Dec 04 '11 at 14:00
  • @David, see the update to my answer above. I find that a less puzzling way to write this. – Mr.Wizard Dec 04 '11 at 19:24
9

Corrected to combine milliseconds with seconds.

You did specify "efficient" and I believe this is two orders of magnitude faster than DateList:

stringDynP[s_String, p_] :=
  StringTake[s, Thread@{{0}~Join~Most@# + 1, #} &@Accumulate@p]

toDateList[string_String] := 
  MapAt[#/1000` &, #, -1] &[
    FromDigits /@ stringDynP[string, {4, 2, 2, 2, 2, 5}]
  ]

toDateList["20110103224832494"]
{2011, 1, 3, 22, 48, 32.494}

stringDynP is a string adaptation of my "Dynamic Partition" function.


Warning for Mathematica 7 users: the DateList method produces a spurious result:

{2011, 1, 12, 9, 23, 24.094}

Presumably in version 8 the following method can be used:

DateList[
 {"20110103224832494",
   {"Year", "Month", "Day", "Hour","Minute", "Second", "Millisecond"}},
 DateDelimiters -> None
]
Community
  • 1
  • 1
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • Of course your result isn't actually a date list that can be used by other functions in Mathematica. :-) – Brett Champion Dec 01 '11 at 19:10
  • @mmorris apparently the behavior of `DateList` was changed between versions 7 and 8, hence my bewilderment. Please see my updated answer for what I believe is a correct and fast method. – Mr.Wizard Dec 01 '11 at 20:09
  • 4
    Looks like a bug in `DateList` in version 7. It seems to get confused when you add minutes into the specification... – Brett Champion Dec 01 '11 at 20:10