Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 983
SKR Sama Avatar asked Apr 16 '26 22:04

SKR Sama


2 Answers

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());
like image 200
Alex Gor Avatar answered Apr 19 '26 11:04

Alex Gor


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

like image 22
SKR Sama Avatar answered Apr 19 '26 10:04

SKR Sama



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!