Need help/tips on converting an ISO 8601 date with the following structure into javascript.
CCYY-MM-DDThh:mm:ssTZD
I'd like to frmat the date like so:
January 28, 2011 - 7:30PM EST
I'd like to keep this solution as clean and minimal as possible.
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:
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/
According to MSDN, the JavaScript Date object does not provide any specific date formatting methods (as you may see with other programming languages). However, you can use a few of the
Date
methods and formatting to accomplish your goal:You could even take it one step further and override the
Date.toString()
method:datejs could parse following, you might want to try out.
Edit: Regex version
Result
Edit2: I changed my timezone to EST and now I got following
return
Basically
return
regex parts just converting above string to your required format.
The Date object handles 8601 as it's first parameter:
If you want to keep it simple, this should suffice:
note parseFloat is must, parseInt doesn't always work. Map requires IE9 or later.
Works for formats:
Not valid for timezones, see other answers about those.