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?
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.