convert string to ISO date time in nodejs

穿精又带淫゛_ 提交于 2021-01-29 12:29:41

问题


Convert this date into ISO format in nodejs

     created_at="September 17th 2019, 16:50:17.000";  
     let new_time = new Date(created_at);
     created_at = new_time.toISOString();
     console.log(created_at);

Output: Invalid Date

Exacting output is in ISO format. like this 2011-10-05T14:48:00.000Z


回答1:


Moment.js is a library which you can use to get the output and do some advanced operations with date and timezones. Below is the code to get expected output.

 var moment = require('moment')

 created_at="September 17th 2019, 16:50:17.000";  
 let new_time = moment("September 17th 2019, 16:50:17.000", "MMMM Do YYYY, HH:mm:ss:SSS");
 created_at = new_time.toISOString();
 console.log(created_at);



回答2:


You will have to pass the date string in below format in order to convert it to ISO date:

var date 1 = "September 17 2019, 16:50:17.000";
let new_date = new Date(date1);
console.lof(new_date);
console.log(new_date.toISOString());


来源:https://stackoverflow.com/questions/58130131/convert-string-to-iso-date-time-in-nodejs

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