SKR Sama
SKR Sama

Reputation: 186

Why Does Date.parse gives 000 milliseconds?

const newDate = Date.now()
const newDate2 = new Date(newDate)
const newDate3 = Date.parse(newDate2)

console.log(newDate);
console.log(newDate2);
console.log(newDate3);

Results:

1638934575678

2021-12-08T03:36:15.678Z

1638934575000

Why does it give 000 milliseconds when it should be 678?

Upvotes: 3

Views: 773

Answers (2)

SKR Sama
SKR Sama

Reputation: 186

using method toISOString() worked for me to get the milliseconds

Upvotes: 1

Alex  Gor
Alex Gor

Reputation: 575

Date.parse - parse date from string. When you pass newDate parse method call .toString() from object and result will be output in format like this - Wed Dec 08 2021 16:37:43 GMT+0200 (Eastern European Standard Time). As you can see - there is no milliseconds. According to this in your case Date.parse not gives it.
You can fix it by using constructor Date(your second example). Or you can use method toISOString() that return milliseconds.

Date.parse(new Date().toISOString());

Upvotes: 3

Related Questions