3

I need to send a Date to my backend in the following format:

2019-04-24T04:27:14.867Z

I tried doing these:

var d = new Date();
console.log(d);

var utcDate1 = new Date(Date.UTC(96, 1, 2, 3, 4, 5));

console.log(utcDate1);

var newFormat = new Date('04/21/19');

console.log(newFormat);

Nothing works. what is the correct approach to get the value as like I requried.

Live Demo.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
user2024080
  • 1
  • 14
  • 56
  • 96

3 Answers3

3

You can use .toISOString() method to get that format. Refer

var newFormat = new Date('04/21/2019');
console.log(newFormat.toISOString());
ellipsis
  • 12,049
  • 2
  • 17
  • 33
2

You can use inbuilt function toISOString() Reference

The toISOString() method returns a string in simplified extended ISO format

var d = new Date();
console.log(d);

var utcDate1 = new Date();

console.log(utcDate1.toISOString());
Kaushik
  • 2,072
  • 1
  • 23
  • 31
2

Use the in-built toISOString method:

var newFormat = new Date('04/21/19').toISOString();

console.log(newFormat);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79