2010-11-18 109 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()構造函數。

此致敬禮。