1

I'm looking for an answer to this exact question: How to format Facebook/Twitter dates (from the JSON feed) in Objective-C only in JS.

Is there a recommended way of doing this?

The date returned from Twitter looks like this: Wed Oct 17 21:22:27 +0000 2012 And from FB it looks like this: 2012-10-18T11:21:30+0000

I find it very strange (and highly annoying) that they don't include a unix timestamp, anyone see a reason for this?

Community
  • 1
  • 1
powerbuoy
  • 12,460
  • 7
  • 48
  • 78
  • 1
    It seems pars-able in JS: http://jsfiddle.net/mgC6m/ – Passerby Oct 30 '12 at 05:16
  • It may be annoying, but as @Passerby points out, it is parseable and therefore can be manipulated just as easily. – Ian Oct 30 '12 at 05:24
  • Oh that's awesome if that works. I want to remember having troubles doing that in IE though. Unable to test right now. – powerbuoy Oct 30 '12 at 05:28
  • 1
    @Passerby—not all browsers in use parse ISO8601 strings correctly, the Twitter format is not consistent with any date standard, nor with ECMA-262. So just because "it works" in the few browsers you might have tried, it should not be trusted. – RobG Oct 30 '12 at 06:27

1 Answers1

4

Date string parsing is a very frequently asked question, there are surely answers already for this. Anyhow...

If the date format is 2012-10-18T11:21:30+0000 (which is pretty much ISO8601) and the offset is always +0000 (i.e. UTC), you can create a suitable date object using:

function isoStringToDate(s) {
  var b = s.split(/[-t:+]/ig);
  return new Date(Date.UTC(b[0], --b[1], b[2], b[3], b[4], b[5]));
}

which will return a local date object set for that UTC time. Note that this depends on the user's system being set to an appropriate timezone.

The Facebook format (Wed Oct 17 21:22:27 +0000 2012) format can be converted similarly:

function fbStringToDate(s) {
  var b = s.split(/[: ]/g);
  var m = {jan:0, feb:1, mar:2, apr:3, may:4, jun:5, jul:6,
           aug:7, sep:8, oct:9, nov:10, dec:11};

  return new Date(Date.UTC(b[7], m[b[1].toLowerCase()], b[2], b[3], b[4], b[5]));
}

Again assuming that the offset it always +0000 (UTC). If there is a need for some other offset, it can be applied to the created date object before returning it.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • This seemed super promising but I get "Invalid Date"... http://jsfiddle.net/MgT8d/ – powerbuoy Oct 30 '12 at 05:33
  • Because you passed the ISO date format to `fbStringToDate` and vice versa. :-) – RobG Oct 30 '12 at 05:43
  • 1
    Lol what a fail :P It works now: http://jsfiddle.net/MgT8d/1/ although `fbStringToDate()` should be called `twitterStringToDate()`. Thanks a lot! I've tested what Passerby suggested though and that works in Chrome. I will keep this code for when I test IE though because I remember it not working there ages ago. – powerbuoy Oct 30 '12 at 05:47
  • 1
    @powerbuoy—don't trust the `Date` constructor or `Date.parse` to correctly parse strings, always do it manually. You will find browsers that don't parse either of those formats, the Twitter one is especially suspect as it isn't compliant with ISO8601 or ECMA-262. – RobG Oct 30 '12 at 06:25