Timestamp mismatch

I played with JavaScript dates, and I'm looking for an explanation regarding the last registered array. Why are the numbers 1352589000, 1352589395 different?

code

 var examples = [ "Fri Jan 16 1970 10:43:09 GMT-0500 (EST)", 1352589395 ]; var text = [ new Date((examples[0])), new Date((examples[1])), ]; var unix = [ new Date((examples[0])).getTime(), new Date((examples[1])).getTime(), ]; console.log(examples); console.log(text); console.log(unix); 

Output

 [ 'Fri Jan 16 1970 10:43:09 GMT-0500 (EST)', 1352589395 ][ 'Fri Jan 16 1970 10:43:09 GMT-0500 (EST)' , 'Fri Jan 16 1970 10:43:09 GMT-0500 (EST)' ][ 1352589000, 1352589395 ] 
+4
source share
3 answers

Numbers are in milliseconds. The difference between them is 395, which is less than half a second. The string format used is reduced to the second, and therefore its millisecond part is 0, but the number you process includes milliseconds (all 395 of them).

+1
source

Because this Unix timestamp is in milliseconds. You did not specify milliseconds, so this gives you exactly 10:43:09 on January 16, 1970. Another time stamp gives you 10: 43: 09.395 of January 16, 1970.

EDIT

The Unix timestamp is the number of SECONDS since January 1, 1970. Javascript getTime () returns the number of MILLISECONDS since January 1, 1970. So yes, this is a Unix timestamp ... in milliseconds.

+1
source

You give two different points to Date (), and both of them are incorrect. The Date Javascript object takes no arguments for the current time or milliseconds or a date string or [year, month, day, hours, minutes, seconds, milliseconds].

"Fri January 16, 1970 10:43:09 GMT-0500 (EST)" is not valid for Date (). For the correct DateString formats, check https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/parse

-1
source

Source: https://habr.com/ru/post/1445274/


All Articles