Parsing ISO 8601 date in Javascript
Need help/tips on converting an ISO 8601 date with the following structure into javascript.
CCYY-MM-DDThh:mm:ssTZD
I'd like to format the date like so:
January 28, 2011 - 7:30PM EST
I'd like to keep this solution as clean and minimal as possible.
Solution 1:
The Date object handles 8601 as it's first parameter:
var d = new Date("2014-04-07T13:58:10.104Z");
console.log(d.toString());
Solution 2:
datejs could parse following, you might want to try out.
Date.parse('1997-07-16T19:20:15') // ISO 8601 Formats
Date.parse('1997-07-16T19:20:30+01:00') // ISO 8601 with Timezone offset
Edit: Regex version
x = "2011-01-28T19:30:00EST"
MM = ["January", "February","March","April","May","June","July","August","September","October","November", "December"]
xx = x.replace(
/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):\d{2}(\w{3})/,
function($0,$1,$2,$3,$4,$5,$6){
return MM[$2-1]+" "+$3+", "+$1+" - "+$4%12+":"+$5+(+$4>12?"PM":"AM")+" "+$6
}
)
Result
January 28, 2011 - 7:30PM EST
Edit2: I changed my timezone to EST and now I got following
x = "2011-01-28T19:30:00-05:00"
MM = {Jan:"January", Feb:"February", Mar:"March", Apr:"April", May:"May", Jun:"June", Jul:"July", Aug:"August", Sep:"September", Oct:"October", Nov:"November", Dec:"December"}
xx = String(new Date(x)).replace(
/\w{3} (\w{3}) (\d{2}) (\d{4}) (\d{2}):(\d{2}):[^(]+\(([A-Z]{3})\)/,
function($0,$1,$2,$3,$4,$5,$6){
return MM[$1]+" "+$2+", "+$3+" - "+$4%12+":"+$5+(+$4>12?"PM":"AM")+" "+$6
}
)
return
January 28, 2011 - 7:30PM EST
Basically
String(new Date(x))
return
Fri Jan 28 2011 19:30:00 GMT-0500 (EST)
regex parts just converting above string to your required format.
January 28, 2011 - 7:30PM EST
Solution 3:
If you want to keep it simple, this should suffice:
function parseIsoDatetime(dtstr) {
var dt = dtstr.split(/[: T-]/).map(parseFloat);
return new Date(dt[0], dt[1] - 1, dt[2], dt[3] || 0, dt[4] || 0, dt[5] || 0, 0);
}
note parseFloat is must, parseInt doesn't always work. Map requires IE9 or later.
Works for formats:
- 2014-12-28 15:30:30
- 2014-12-28T15:30:30
- 2014-12-28
Not valid for timezones, see other answers about those.
Solution 4:
Maybe, you can use moment.js which in my opinion is the best JavaScript library for parsing, formatting and working with dates client-side. You could use something like:
var momentDate = moment('1890-09-30T23:59:59+01:16:20', 'YYYY-MM-DDTHH:mm:ss+-HH:mm:ss');
var jsDate = momentDate.toDate();
// Now, you can run any JavaScript Date method
jsDate.toLocaleString();
The advantage of using a library like moment.js is that your code will work perfectly even in legacy browsers like IE 8+.
Here is the documenation about parsing methods: https://momentjs.com/docs/#/parsing/