Reputation: 1417
I have got a date in this format as a variable: Friday, August 5th 2016, 09:00
and need to convert it to this format 2016,8,5,09,00,0,0,0
for my calendar file. Is there any simple way of doing it with jQuery? I have tried:
var oldDate = "Friday, August 5th 2016, 09:00";
var newDate = new Date(yyyy,mm,dd,HH,mm,0,0,0);
It didn't help. I really appreciate your time on sorting this out for me.
Upvotes: 1
Views: 29
Reputation: 78570
First of all, your date format is wrong. The th
is throwing javascript off when it tries to parse it. You might want to consider changing the format that gets saved out to the server. For this demo, I've just written a regular expression to parse it out. After that it's pretty easy to get it to the right format.
var oldDate = new Date("Friday, August 5th 2016, 09:00".replace(/(\d)+(st|nd|rd|th)/g, '$1'));
var newDate = [
oldDate.getFullYear(),
oldDate.getMonth() + 1,
oldDate.getDate(),
('0' + oldDate.getHours()).slice(-2),
('0' + oldDate.getMinutes()).slice(-2),
0,
0,
0
];
var formattedDate = newDate.join(',');
document.write(formattedDate);
Upvotes: 5