Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Does Mathematica have a built-in function to convert ISO 8601 formatted date strings (e.g. 2012-01-04T00:00:00) to a date list?

ISO 8601 strings come in a variety of formats:

Date alone: 2012-12-03
Combined date and time: 2012-12-03T22:02
Combined date and time with time zone: 2012-12-03T22:02:47.23+01

On top of that individual components can be omitted (see the Wiki page). I am looking for a solution that covers the whole ISO 8601 standard.

share|improve this question
    
Unfortunately I think you need to roll your own in that case. –  Szabolcs Dec 3 '12 at 21:12
1  
Whhooaaa, I'm just realizing you are the sakra! The one who was so kind to implement a findMathematica for cmake ;-) You cannot imagine how easy life is with your tool. –  halirutan Dec 4 '12 at 1:18
add comment

3 Answers 3

up vote 5 down vote accepted

You can use JLink (as already mentioned while I read JavaDoc). Searching StackOverflow brings up this highly voted answer

Converting ISO8601-compliant String to java.util.Date

I don't know whether it covers the whole standard but this works out of the box. If this is not sufficient, you can use an equivalent piece of code written for Joda Time

Needs["JLink`"]

ParseDateString[date_String] := JavaBlock[
  InstallJava[];
  LoadJavaClass["javax.xml.bind.DatatypeConverter", 
   StaticsVisible -> True];
  DateList[
   javax`xml`bind`DatatypeConverter`parseDateTime[date]@getTime[]@toString[]
  ]
]

This uses the DataypeConverter class which is available without in standard Java 6. It converts it into a standard date string which can then be used with DateList.

ParseDateString["2010-01-01T12:00:00Z"]

(* {2010, 1, 1, 13, 0, 0.} *)
share|improve this answer
    
Does this cover the whole ISO 8601 standard? The o/p wrote: "I am looking for a solution that covers the whole ISO 8601 standard." –  Andreas Lauschke Dec 3 '12 at 23:20
    
Probably not, but I want a working example here. To put another jar in the classpath and write your own method is easy from this point IMO. –  halirutan Dec 3 '12 at 23:36
add comment

Use Joda Time with JLink. Joda is fully 8601-compliant, and with JLink the whole Java universe is immediately at your fingertips from M.

I've posted a few replies in this forum using Joda Time, for example Faster alternatives for DayOfWeek

where I've mentioned that Joda Time is fully 8601-compliant.

share|improve this answer
add comment

I want to build upon @halirutan answer.

The reason why I need to build upon the answer is DatatypeConverter`parseDateTime actually returns in the following format "Mon Mar 10 21:24:10 GMT+08:00 2014", and DateList[] can't automatically interpret the returned date

Hence, I need to provide the explicit format to DateList[] to make it work in Mathematica. The explicit format is "{"DayName"," ","MonthName"," ","Day"," ", "Hour",":","Minute",":","Second"," GMT+08:00 ", "Year"}"

Please take note of the Time Zone portion. My time zone is GMT+08:00, hence I hard coded it in the explicit format. Most likely you need to update to your own time zone for it to work successfully. For example, your time zone is GMT-08:00 if you are in USA West Coast.

Needs["JLink`"]

ParseDateString[date_String] := JavaBlock[
  InstallJava[];
  LoadJavaClass["javax.xml.bind.DatatypeConverter", 
   StaticsVisible -> True];
  DateList[{
   javax`xml`bind`DatatypeConverter`parseDateTime[date]@getTime[]@toString[],
   {"DayName", " ", "MonthName", " ", "Day", " ", "Hour", ":", "Minute",":", "Second", " GMT+08:00 ", "Year"}}
  ]
]

ParseDateString["2014-03-10T09:24:10.000-04:00"]

And the result will be

{2014, 3, 10, 21, 24, 10.}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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