2010-11-18 112 views
1

在JavaScript中,如何在UNIX时间(即当前时间+1小时)指定任何未来时间?Javascript:未来时间为UNIX时间戳

+1

可能希望从[此SO问题]开始(http://stackoverflow.com/questions/221294/how-do-you-get-a-timestamp-in-javascript)。 – 2010-11-18 09:44:14

回答

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 

Google helped me easily ...

5

你需要这样做:

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 

在你以毫秒为单位的UNIX时间戳,并将其转换成秒的第一线,之后你可以添加或。减去秒,就像在第二行一样。要转换回日期,只需要乘以时间戳* 1000(再次获得毫秒)并将其传递给Date()构造函数。

此致敬礼。