Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Date
  6. setTime()

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⁩.

ThesetTime() method ofDate instances changes thetimestamp for this date, which is the number of milliseconds since theepoch, defined as the midnight at the beginning of January 1, 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

Syntax

js
setTime(timeValue)

Parameters

timeValue

An integer representing the new timestamp — the number of milliseconds since the midnight at the beginning of January 1, 1970, UTC.

Return value

Changes theDate object in place, and returns its newtimestamp. IftimeValue isNaN (or other values that getcoerced toNaN, such asundefined), the date is set toInvalid Date andNaN is returned.

Examples

Using setTime()

js
const theBigDay = new Date("1999-07-01");const sameAsBigDay = new Date();sameAsBigDay.setTime(theBigDay.getTime());

Specifications

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

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp