setTime() 方法用來以「自 1970 年 1 月 1 日 00:00:00 UTC 以來的毫秒數」設定 Date 物件所表示的時間。
Date.setTime(timeValue)
參數說明:
・timeValue:一個整數,表示自 1970 年 1 月 1 日 00:00:00 UTC 起的毫秒數。
此方法沒有回傳值(標示為 NA)。
<html>
<head>
<title>JavaScript setTime Method</title>
</head>
<body>
<script type="text/javascript">
var dt = new Date("Aug 28, 2008 23:30:00");
dt.setTime(5000000);
document.write(dt);
</script>
</body>
</html>
執行後會輸出:
Thu Jan 1 06:53:20 UTC+0530 1970
由於 5000000 毫秒是從 1970 起算的極早時間點,因此結果落在 1970 年,這也說明了 setTime() 是以「時間戳」為基準來設定時間。