问题
I have a string like this:
21.03.2016 23:59
And I need this string converted into a ISO-8601 date-time string:
YYYY-MM-DDTHH:mm:ss+00:00
Is there a simple way to convert this date? I try it whit moment.js but i can't find a function to parse an existing date.
回答1:
You can also do this without using moment.js. Look code as following:
(new Date("03.21.2016 23:59")).toISOString()
just you need to change your string 21.03.2016 23:59 (dd-mm-yyyy) to 03.21.2016 23:59 (mm-dd-yyyy). You can easily do this by split the date and change the order of split part.
And if you dont want to do this then simply use moment.js as per matthias's answer.
回答2:
Using moment.js you could do:
var dateString = '21.03.2016 23:59';
var momentDate = moment(dateString, 'DD.MM.YYYY HH:mm');
console.log(momentDate.toISOString());
Here is a fiddle showing this.
来源:https://stackoverflow.com/questions/36126436/javascript-reformat-date-string-to-iso8601