Javascript - reformat date string to ISO8601

江枫思渺然 提交于 2019-12-13 02:13:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!