Reputation: 1957
i try to build a function that converts every date to a timestamp.
JS
$(document).ready(function()
{
var date1 = dateParser('Fri, 20 Jan 2012 09:30:02 +0000');
$('body').append('date1 -> '+date1.timeStamp +' / '+date1.isoDate + '<br />');
var date2 = dateParser('2012-01-19T20:34:15+0000');
$('body').append('date2 -> '+date2.timeStamp +' / '+date2.isoDate + '<br />');
});
function ISODateString(d) { function pad(n){ return n<10 ? '0'+n : n }
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate())+'T'
+ pad(d.getUTCHours())+':'
+ pad(d.getUTCMinutes())+':'
+ pad(d.getUTCSeconds())+'Z'
}
function dateParser(someDate) {
if(typeof someDate == 'string') {
var timeStamp = Date.parse(someDate) / 1000;
var isoDate = ISODateString(new Date( timeStamp*1000 ));
}
if(typeof someDate == 'number') {
var timeStamp = someDate;
var isoDate = ISODateString(new Date( someDate*1000 ));
}
return {"timeStamp": timeStamp, "isoDate": isoDate };
}
example: http://jsfiddle.net/L6uce/6/
The first date works but the second returns NaN / NaN-NaN-NaNTNaN:NaN:NaNZ. Any ideas? Is there maybe a better (easier) way?
Thanks in advance!
Upvotes: 2
Views: 1424
Reputation: 7722
Look here to learn more about ISO: http://en.wikipedia.org/wiki/ISO_8601
Substitute "+0000" with Z and it works.
"Combining date and time representations to represent a single point in time (time point) is quite simple. It is in the format of T where the time designator [T] is used to show the start of the time component of the representation. Both the date and time components of the time point are any proper representations of date and time specified by the standard. For example, "2007-04-05T14:30" uses the extended formats for date and time [YYYY]-[MM]-[DD]T[hh]:[mm], while "20090621T0545Z" uses the basic formats [YYYY][MM][DD]T[hh][mm]Z."
Upvotes: 2