In JavaScript, how to specify any future time in UNIX time (ie, current time + 1 hour)?
Asked
Active
Viewed 5,582 times
1
-
1May want to start with [this SO question](http://stackoverflow.com/questions/221294/how-do-you-get-a-timestamp-in-javascript). – Marcus Whybrow Nov 18 '10 at 09:44
2 Answers
5
You need to do this:
var timestamp = Math.round(new Date().getTime() / 1000); #get timestamp for now
timestamp += 3600; #now + 1h
var datetime = new Date(timestamp*1000); #convert back to date object
In the first line you get the UNIX timestamp in miliseconds and convert it to seconds, after you can add or substract seconds, just like in the second line. To convert back to date you just need to multiply the timestamp * 1000 (to get miliseconds again) and pass it to the Date() constructor.
Best regards.

SubniC
- 9,807
- 4
- 26
- 33
1
var foo = new Date; // Generic JS date object
var unixtime_ms = foo.getTime(); // Returns milliseconds since the epoch
var future_unixtime_ms = unixtime_ms + 60 * 60 * 1000; // 60 seconds per minute, 1000 ms per second

Vincent Mimoun-Prat
- 28,208
- 16
- 81
- 124