The valueOf()
method of Date
instances returns the number of milliseconds for this date since the epoch, which is defined as the midnight at the beginning of January 1, 1970, UTC.
The valueOf()
method of Date
instances returns the number of milliseconds for this date since the epoch, which is defined as the midnight at the beginning of January 1, 1970, UTC.
valueOf()
None.
The valueOf()
method is part of the type coercion protocol. Because Date
has a [@@toPrimitive]()
method, that method always takes priority over valueOf()
when a Date
object is implicitly coerced to a number. However, Date.prototype[@@toPrimitive]()
still calls this.valueOf()
internally.
The Date
object overrides the valueOf()
method of Object
. Date.prototype.valueOf()
returns the timestamp of the date, which is functionally equivalent to the Date.prototype.getTime()
method.
const d = new Date(0); // 1970-01-01T00:00:00.000Z console.log(d.valueOf()); // 0
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
valueOf |
1 | 12 | 1 | 3 | 1 | 18 | 4 | 10.1 | 1 | 1.0 | 4.4 | 1.0 | 0.10.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf