How I can format a date string using Javascript. For example:
var date_str = "2010-10-06T03:39:41+0000";
The result should be like this:
11:39 AM Oct 6th
Is there any ideas on this? Note: date_str is an example of returned date from Facebook Graph API.
Thanks in advance.
Parsing the date shouldn’t be too difficult – all the components are in the right order so you just need to split on the special characters, subtract 1 from the month value and apply the array, minus the last element, to Date.UTC():
This gives us a Date object for the date specified, the rest is just formatting each component back into a string:
If you want the output to be the same regardless of timezone (the original time supplied), you need to swap the methods getHours(), getMinutes(), etc for getUTCHours(), getUTCMinutes(), etc.