setSeconds() 方法用來依據本地時間,設定指定日期物件上的「秒」,並可選地一併設定毫秒。
Date.setSeconds(secondsValue[, msValue])
參數說明:
・secondsValue:0 到 59 的整數
・msValue:0 到 999 的整數,表示毫秒
若不指定 msValue,則沿用 getMilliseconds 目前回傳的值。當傳入的值超出預期範圍時,setSeconds 會相應更新日期物件——例如 secondsValue 為 100 時,分鐘會加 1,並以 40 作為秒。
此方法沒有回傳值(標示為 NA)。
<html>
<head>
<title>JavaScript setSeconds Method</title>
</head>
<body>
<script type="text/javascript">
var dt = new Date("Aug 28, 2008 23:30:00");
dt.setSeconds(80);
document.write(dt);
</script>
</body>
</html>
執行後會輸出:
Thu Aug 28 23:31:20 UTC+0530 2008
範例中傳入 80 秒,由於超過 59,JavaScript 自動進位:分鐘加 1、秒變為 20,充分展現了日期物件的自動進位特性。