I have a date string "2012-11-14T06:57:36+0000" that I want to convert to the following format "Nov 14 2012 12:27". I have tried a lot of solutions including Convert UTC Date to datetime string Javascript. But nothing could help me. The following code worked for me in android. But for ios it displays as invalid date
var date = "2012-11-14T06:57:36+0000";
//Calling the function
date = FormatDate(date);
//Function to format the date
function FormatDate(date)
{
var newDate = new Date(date);
newDate = newDate.toString("MMMM");
return (newDate.substring(4,21));
}
Can anyone help me? Thanks in advance
I have a date string "2012-11-14T06:57:36+0000" that I want to convert to the following format "Nov 14 2012 12:27". I have tried a lot of solutions including Convert UTC Date to datetime string Javascript. But nothing could help me. The following code worked for me in android. But for ios it displays as invalid date
var date = "2012-11-14T06:57:36+0000";
//Calling the function
date = FormatDate(date);
//Function to format the date
function FormatDate(date)
{
var newDate = new Date(date);
newDate = newDate.toString("MMMM");
return (newDate.substring(4,21));
}
Can anyone help me? Thanks in advance
Share Improve this question edited May 23, 2017 at 11:54 CommunityBot 11 silver badge asked Nov 14, 2012 at 13:47 AnandAnand 5,3305 gold badges46 silver badges59 bronze badges2 Answers
Reset to default 6All browsers doesn't support the same date formats. The best approach we can choose is to split the string on the separator characters -, and : , and pass each of the resulting array items to the Date constructor, see the following function
function FormatDate(date)
{
var arr = date.split(/[- :T]/), // from your example var date = "2012-11-14T06:57:36+0000";
date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], 00);
newDate = date.toString("MMMM");
//.. do further stuff here
}
You can get a Date
object by initializing a new date:
var date = "2012-11-14T06:57:36+0000";
var newDate = new Date(date); // this will parse the format
console.log(newDate);
> Wed Nov 14 2012 01:57:36 GMT-0500 (EST)
As for the formatting, there are several threads (like this one) on that already.
There are also libraries for date formatting and processing that people usually end up using when doing a lot of date processing. I would suggest Datejs if you're looking for something like that.