# Change string date/time to be understandable for mathematica

I'm trying to convert a string of a data and time to be understandable for mathematica (I want to make time related plots).

However, I can get my head around how to do this.

For example:

temp = "Sun Jun 01 22:00:21 +0000 2014";


I have tried the following commands (and many more variations):

DateList[{temp, {"DayName", "Month", "Day", "Hour", "Minute",
"Second", "TimeZone", "Year"}}];
DateList[{temp, {"DayName", "Month", "Day", "Hour", ":", "Minute",
":", "Second", "TimeZone", "Year"}}]


Both gave me this type of error:

DateString::str: "String \!$$\"Sun Jun 01 22:00:21 +0000 2014\"$$ cannot be interpreted as a date in format {\!$$\"DayName\", \"Month\", \"Day\", \"Hour\", \"Minute\", \"Second\", \"TimeZone\", \"Year\"$$}."


Can anyone tell what I'm doing wrong? Thank you!!

• Hi @Lena, welcome to Mathematica.SE, please consider taking the tour so you learn the basic rules of the site. Once you gain enough reputation by making good questions you will be able to vote up and down both questions and answers. Your question has been answered, but its a good idea to wait a few hours for other answers before accepting the best one for you. Oct 21, 2014 at 15:26

The problem is with "TimeZone", that its not defined, and with using "Month" instead of "MonthName".

This works:

DateList[{"Sun Jun 01 22:00:21 2014", {"DayName", "MonthName", "Day", "Hour", "Minute", "Second", "Year"}}]


Are the strings in temp with fixed format? Then removing the characters will do

DateList[{StringDrop[temp, {21, 26}], {"DayName", "MonthName", "Day", "Hour", "Minute", "Second", "Year"}}]


Otherwise

DateList[ToString@ImportString[temp, "Table"][[1, {6, 2, 3, 4}]]]


I'm sure somebody else can work some magic to make this shorter and more efficient.

• For me these commands do not work, and I don't quite understand why. If I use exactly your commands, I get this error: DateList[{"Sun Jun 01 22:00:21 2014", {"DayName", "MonthName", "Day", "Hour", "Minute", "Second", "Year"}}] DateString::str: String Sun Jun 01 22:00:21 2014 cannot be interpreted as a date in format {DayName,MonthName,Day,Hour,Minute,Second,Year}. >>
– Lena
Oct 23, 2014 at 8:04
• @Lena, Strange, they do work for me. Anyhow the answer by Eldo is to my taste the most thorough and elegant. You should consider learning from that one, and even accepting it if you think it does answer your original question. (Learn here why is that important) Oct 23, 2014 at 8:12

Some options

date = "Sun Jun 01 22:00:21 +0000 2014";


Eliminate wrong entry

rep = StringReplace[date, "+0000" -> ""]


"Sun Jun 01 22:00:21 2014"

Convert to List

list = rep // DateList


{2014, 6, 1, 22, 0, 21.}

Find day

day = DayName[list]


Sunday

Consolidate

{day}~Join~list


{Sunday, 2014, 6, 1, 22, 0, 21.}

Convert to DateObject @ GMT

obj = DateObject[list, TimeZone -> 0]


tzc = TimeZoneConvert[obj, $TimeZone]  Transform to DateList tzc // DateList  {2014, 6, 2, 0, 0, 21.} Convert to another CalendarType DateObject[list, CalendarType -> "Jewish"]  Manually fixing the timezone. datecalc[t_] := Module[{}, t2 = StringSplit[t, " " | ":"]; tzsign = StringTake[t2[[7]], 1]; tzhrs = ToExpression[StringTake[t2[[7]], {2, 3}]]; tzmin = ToExpression[StringTake[t2[[7]], -2]]; {hour, min} = ToExpression[t2[[{4, 5}]]]; If[SameQ[tzsign, "+"], h2 = hour + tzhrs; m2 = min + tzmin, h2 = hour - tzhrs; m2 = min - tzmin]; ymd = Take[DateList[{StringJoin[ Riffle[Append[Take[t2, {2, 3}], Last@t2], " "]], {"MonthName", "Day", "Year"}}], 3]; DateList[Join[ymd, {h2, m2}, {ToExpression[t2[[6]]]}]]]  Works for UTC-02:30 datecalc["Sun Jun 01 22:00:21 -0230 2014"]  {2014, 6, 1, 19, 30, 21.} and UTC+13:45 datecalc["Fri Dec 31 11:00:21 +1345 1999"]  {2000, 1, 1, 0, 45, 21.} At first I tried this hack: ClearAll; temp = "Sun Jun 01 22:00:21 +0000 2014"; td = If[StringFreeQ[temp, "-"], DateList[{temp, {"DayNameShort", " ", "MonthNameShort", " ", "Day", " ", "Hour24", ":", "Minute", ":", "Second", " +", "Millisecond", " ", "Year"}}], DateList[{temp, {"DayNameShort", " ", "MonthNameShort", " ", "Day", " ", "Hour24", ":", "Minute", ":", "Second", " -", "Millisecond", " ", "Year"}}] ]; tx = If[StringFreeQ[temp, "-"], DateList[td, TimeZone -> Plus[(FractionalPart[Last[td]]* 100),$TimeZone] ],
TimeZone -> Plus[(-FractionalPart[Last[td]]* 100), $TimeZone]]] (* {2014, 6, 1, 22, 0, 21.} *)  Since there is no "TimeZone" pattern match expression, I hacked the "Millisecond". However, millisecond is limited to 3 digits, so we get round off errors. Next I tried this: ClearAll; temp = "Sun Jun 01 22:00:21 +0000 2014"; DateList[{StringDrop[ StringRotateRight[StringInsert[temp, " ", -1], 5], -7], {"Year", " ", "DayNameShort", " ", "MonthNameShort", " ", "Day", " ", "Hour24", ":", "Minute", ":", "Second"}}, TimeZone -> Plus[(ToExpression[ StringJoin[ StringTrim[ StringTake[ StringRotateRight[StringInsert[temp, " ", -1], 5], {-7, -4}]], StringTrim[ ToString[ N[Rationalize[ ToExpression[ StringTrim[ StringTake[ StringRotateRight[StringInsert[temp, " ", -1], 5], {-3, -1}]]]/60]]], "0"] ]]),$TimeZone]]