malik
malik

Reputation: 41

convert string to ISO date time in nodejs

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

Upvotes: 1

Views: 3997

Answers (2)

Sunny Parekh
Sunny Parekh

Reputation: 983

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

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

Upvotes: 3

Sanket Phansekar
Sanket Phansekar

Reputation: 741

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);

Upvotes: 4

Related Questions