Date.prototype.valueOf()
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.
valueOf()
方法返回一个 Date
对象的原始值。
尝试一下
const date1 = new Date(Date.UTC(96, 1, 2, 3, 4, 5));
console.log(date1.valueOf());
// Expected output: 823230245000
const date2 = new Date("02 Feb 1996 03:04:05 GMT");
console.log(date2.valueOf());
// Expected output: 823230245000
语法
dateObj.valueOf()
返回值
从 1970 年 1 月 1 日 0 时 0 分 0 秒(UTC,即协调世界时)到该日期的毫秒数。
描述
valueOf
方法返回以数值格式表示的一个 Date
对象的原始值,从 1970 年 1 月 1 日 0 时 0 分 0 秒(UTC,即协调世界时)到该日期对象所代表时间的毫秒数。
该方法的功能和 Date.prototype.getTime()
方法一样。
该方法通常在 JavaScript 内部被调用,而不是在代码中显式调用。
示例
使用 valueOf()
js
var x = new Date(56, 6, 17);
var myVar = x.valueOf(); // assigns -424713600000 to myVar
规范
Specification |
---|
ECMAScript® 2025 Language Specification # sec-date.prototype.valueof |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
valueOf |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.