Date.prototype.setTime()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

setTime() 方法將從 1970 年 01 月 01 日 00:00:00 UTC 起所經過的毫秒數設置為 Date 物件的值。

嘗試一下

const launchDate = new Date("July 1, 1999, 12:00:00");
const futureDate = new Date();
futureDate.setTime(launchDate.getTime());

console.log(futureDate);
// Expected output: "Thu Jul 01 1999 12:00:00 GMT+0200 (CEST)"

const fiveMinutesInMillis = 5 * 60 * 1000;
futureDate.setTime(futureDate.getTime() + fiveMinutesInMillis);

console.log(futureDate);
// Expected output: "Thu Jul 01 1999 12:05:00 GMT+0200 (CEST)"
// Note: your timezone may vary

語法

js
setTime(timeValue)

參數

timeValue

一個整數,表示自 1970 年 01 月 01 日 00:00:00 UTC 起所經過的毫秒數。

返回值

1970 年 01 月 01 日 00:00:00 UTC 與更新日期之間的毫秒數(實際上是參數值)。

描述

setTime() 方法指派一日期與時間至另一 Date 物件。

範例

使用 setTime()

js
const theBigDay = new Date("July 1, 1999");
const sameAsBigDay = new Date();
sameAsBigDay.setTime(theBigDay.getTime());

規範

Specification
ECMAScript® 2025 Language Specification
# sec-date.prototype.settime

瀏覽器相容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
setTime

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

參見