简体   繁体   中英

Javascript - reformat date string to ISO8601

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM