0

I have a date, as returned from a JSON, in the following format:

YYYYMMDDThhmmssZ

and I want to parse it in Javascript. I found some resources that cataloge this format as ISO-8601 basic format, which is slightly different from the extended format which looks like this:

YYYY-MM-DDThh:mm:ssZ

I have found some resources for how to parse the extended format, but so far I haven't found anything for parsing the basic format. Does such functionality exist in the Date module of Javascript, or so I have to use other modules (e.g. Moment)? I am asking because this is not really an option for me, as the application I am developing is a gnome-shell extension and I don't want to have any extra dependencies.

Nakano
  • 45
  • 2
  • 9
  • Is parsing it as a string an option? – Kyle Becker May 10 '17 at 16:39
  • What does this have to do with "gnome-shell-extensions"?!? – Heretic Monkey May 10 '17 at 16:56
  • @kyle, no, parsing it as a string is easy. I want it in a Date format, so that I can manipulate it accordingly (i.e. subtract other Dates from it). – Nakano May 10 '17 at 19:25
  • @mike-mccaughan, I need this for a gnome-shell extension (as it is mentioned in the question). I think it is an important piece of information as it emphasises the fact that I don't want to use any extra javascript modules (such as moment.js) – Nakano May 10 '17 at 19:26
  • You should consider mentioning that in the title and/or at the top of the question. The title alone is pretty generic (and a duplicate [of several questions](http://stackoverflow.com/q/10638529/215552)). – Heretic Monkey May 10 '17 at 19:34
  • @MikeMcCaughan I guess you are right. I will mark my question as duplicate. I had seen these questions earlier, but thought that maybe there is something else existing. – Nakano May 12 '17 at 13:25

1 Answers1

1

Update

If you can't use moment.js and you can rely on the format of your input being as described, then consider parsing it yourself with something like this:

function parseDate(input) {
  return new Date(Date.UTC(
    parseInt(input.slice(0, 4), 10),
    parseInt(input.slice(4, 6), 10) - 1,
    parseInt(input.slice(6, 8), 10),
    parseInt(input.slice(9, 11), 10),
    parseInt(input.slice(11, 13), 10),
    parseInt(input.slice(13,15), 10)
  ));
}

console.log(parseDate('20130208T080910Z'));

It's fairly straightforward to slice out the composite parts. The only quirks are that January is the 0-th month (hence the - 1) and that the Date constructor assumes your parameters are for local time, so we follow the advice given in the Date documentation:

Note: Where Date is called as a constructor with more than one argument, the specifed arguments represent local time. If UTC is desired, use new Date(Date.UTC(...)) with the same arguments.

I left out format checking, but if you need it, you could define a regular expression to check your input (I'd recommend something no more complex than /^[0-9]{8}T[0-9]{6}Z$/, and then just let Date.UTC(...) do its thing).

Old answer, updated to specify format string explicitly

If you can use moment.js, it already supports parsing this format:

console.log(moment('20130208T080910Z', 'YYYYMMDDTHHmmssZ'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script>
tavnab
  • 2,594
  • 1
  • 19
  • 26