I'm sure there's a reason I have to add three zeros to every Unix timestamp in JavaScript in order to get the correct date. Can you tell me why? Is it as simple as milliseconds since the epoch vs. seconds?
Asked
Active
Viewed 2.0k times
34
-
1Because they use different units of measurement? – spender Jan 13 '11 at 02:27
-
Thanks everyone. I knew it had to be something logical. My next question will be as to what might be the design decisions around milliseconds vs. seconds given the semi-inconsistency. – buley Jan 13 '11 at 02:30
-
How about you first upvote helpful answers and accept the answer that best solved your question? – Phrogz Jan 13 '11 at 02:31
-
Of course! After waiting through the bulk of the wait period (you can't accept answers right away), I was away from my computer for a short period. – buley Jan 13 '11 at 02:55
-
The best part is that by the time I just accepted my answer, some community members had helped me choose :) – buley Jan 13 '11 at 02:55
-
I wish i could downvote comments :( you guys didn't add anything of value or answer any of his questions. thanks for pointing out that they are different. Wow. useful, that's why he asked and I ended up here. Still no clue why. – edthethird May 01 '14 at 16:10
3 Answers
56
Because Javascript uses milliseconds internally, while normal UNIX timestamps are usually in seconds.

deceze
- 510,633
- 85
- 743
- 889
-
Multiplication shouldn't lose anything, even Javascript's weird not-a-float-is-a-float `Number` type should be able to do that without rounding errors. Division on the other hand will obviously at least lose you the millisecond part, which regular UNIX timestamps can't express anyway. – deceze Nov 09 '17 at 20:19
5
Javascript uses the number of milliseconds since epoch. Unix timestamp is seconds since epoch.
Hence, the need to convert Unix timestamp into millseconds before using it in Javascript

Dancrumb
- 26,597
- 10
- 74
- 130
4
Unix time is the number of seconds since the epoch (1 Jan 1970). In Javascript, the Date
object expects the number of milliseconds since the epoch, hence the 1000-fold difference.

Cameron Skinner
- 51,692
- 2
- 65
- 86