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.
Phương thức setTime() của các instance Date thay đổi timestamp cho ngày này, là số mili giây kể từ epoch, được định nghĩa là nửa đêm đầu ngày 1 tháng 1 năm 1970, UTC.
Try it
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 fiveMinutesInMs = 5 * 60 * 1000;
futureDate.setTime(futureDate.getTime() + fiveMinutesInMs);
console.log(futureDate);
// Expected output: "Thu Jul 01 1999 12:05:00 GMT+0200 (CEST)"
// Note: your timezone may vary
Cú pháp
js
setTime(timeValue)
Tham số
timeValue-
Một số nguyên đại diện cho timestamp mới — số mili giây kể từ nửa đêm đầu ngày 1 tháng 1 năm 1970, UTC.
Giá trị trả về
Thay đổi đối tượng Date tại chỗ và trả về timestamp mới của nó. Nếu timeValue là NaN (hoặc các giá trị khác bị ép kiểu thành NaN, chẳng hạn như undefined), ngày sẽ được đặt thành Invalid Date và NaN được trả về.
Ví dụ
>Sử dụng setTime()
js
const theBigDay = new Date("1999-07-01");
const sameAsBigDay = new Date();
sameAsBigDay.setTime(theBigDay.getTime());
Đặc tả kỹ thuật
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-date.prototype.settime> |