3

I'd Like to generate some unix Timestamp in the future. therefore i use a generator like unixtimestamp.com. But when i use the any generated timestamp for example in a console.log in chrome or firefox it produces not the timestamp from now but a timestamp from 1970.

Example: 1462277206 is the Unix Timestamp for Tue May 3 12:06:46 2016 GMT.

But in the console new Date(1462277206) returns Sat Jan 17 1970 23:11:17 GMT+0100 (CET).

What am i doing wrong?

Thanks for your help!

Muff

Raggamuffin
  • 699
  • 1
  • 6
  • 19

1 Answers1

12

JavaScript date/time numbers are milliseconds since The Epoch, not seconds since The Epoch like old-style Unix Epoch values. If you have a value in seconds, multiply by 1000:

new Date(1462277206 * 1000);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    Thanks - I just wonder why this is not uniformed so that the unix timestamp is generally served in milliseconds. – Raggamuffin May 03 '16 at 11:22
  • 2
    @Raggamuffin: History. For years, time values were measured in seconds since The Epoch. This started in the 1970s. Then we felt the need for more precision and more modern systems started using milliseconds since The Epoch. Now there are even more modern systems using even more precise systems, such as Microsoft's "ticks since January 1, 0001 at 00:00:00.000 in the Gregorian calendar" (a "tick" being 100 nanoseconds). Early Unix hackers wouldn't have dreamed of using the amount of storage for a time value that C# uses for `DateTime`, but things have moved on a bit in the last ~45 years. – T.J. Crowder May 03 '16 at 11:36