Home > Blockchain >  Why Does Date.parse gives 000 milliseconds?
Why Does Date.parse gives 000 milliseconds?

Time:12-09

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?

CodePudding user response:

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

CodePudding user response:

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

  • Related