非常教程

JavaScript参考手册

日期 | Date

Date.prototype

Date.prototype属性表示Date构造函数的原型。

| Property attributes of Date.prototype |

|:----|

| Writable | no |

| Enumerable | no |

| Configurable | yes |

描述

JavaScript Date实例继承自Date.prototype。您可以修改构造函数的原型对象以影响由JavaScript Date实例继承的属性和方法。

为了与计算兼容(换句话说,考虑到2000年),您应该始终指定年份; 例如,1998年使用,而不是98为了帮助您指定完整的一年,包括JavaScript的方法getFullYear()setFullYear()getUTCFullYear()setUTCFullYear()

从ECMAScript 6开始,Date.prototype对象本身就是一个普通对象。这不是一个Date实例。

属性

Date.prototype.constructor返回创建实例的函数。这是Date默认的构造函数。

方法

Getter

Date.prototype.getDate()根据当地时间返回指定日期的月份(1-31)。Date.prototype.getDay()根据当地时间返回指定日期的星期几(0-6)。Date.prototype.getFullYear()根据当地时间返回指定日期的年份(4位数字的4位数字)。Date.prototype.getHours()根据当地时间返回指定日期的小时(0-23)。Date.prototype.getMilliseconds()根据当地时间返回指定日期的毫秒数(0-999)。Date.prototype.getMinutes()根据当地时间返回指定日期的分钟数(0-59)。Date.prototype.getMonth()根据当地时间返回指定日期中的月份(0-11)。Date.prototype.getSeconds()根据当地时间返回指定日期的秒数(0-59)。Date.prototype.getTime()以UTC时间1970年1月1日00:00:00(以前时间为负)的毫秒数返回指定日期的数值。Date.prototype.getTimezoneOffset()以分钟为单位返回当前语言环境的时区偏移量。Date.prototype.getUTCDate()根据世界时间返回指定日期中月份(1-31)的日期(日期)。Date.prototype.getUTCDay()根据世界时间返回指定日期中的星期几(0-6)。Date.prototype.getUTCFullYear()根据世界时间返回指定日期的年份(4位数字的4位数字)。Date.prototype.getUTCHours()根据世界时间返回指定日期的小时数(0-23)。Date.prototype.getUTCMilliseconds()根据通用时间返回指定日期的毫秒数(0-999)。Date.prototype.getUTCMinutes()根据世界时间返回指定日期的分钟数(0-59)。Date.prototype.getUTCMonth()根据世界时间返回指定日期中的月份(0-11)。Date.prototype.getUTCSeconds()根据世界时间返回指定日期的秒数(0-59)。Date.prototype.getYear()根据当地时间返回指定日期的年份(通常为2-3位数字)。getFullYear()改为使用。

Setter

Date.prototype.setDate()根据当地时间设置指定日期的月份的日期。Date.prototype.setFullYear()根据当地时间设置指定日期的全年(例如4位数的4位数字)。Date.prototype.setHours()根据当地时间设置指定日期的小时数。Date.prototype.setMilliseconds()根据当地时间设置指定日期的毫秒数。Date.prototype.setMinutes()根据当地时间设置指定日期的分钟数。Date.prototype.setMonth()根据当地时间设置指定日期的月份。Date.prototype.setSeconds()根据当地时间设置指定日期的秒数。Date.prototype.setTime()Date对象设置为自1970年1月1日UTC时间00:00:00以毫秒为单位表示的时间,允许以前的时间为负数。Date.prototype.setUTCDate()根据世界时间设置指定日期的月份的日期。Date.prototype.setUTCFullYear()根据通用时间设置指定日期的全年(例如4位数的4位数字)。Date.prototype.setUTCHours()根据通用时间设置指定日期的小时数。Date.prototype.setUTCMilliseconds()根据通用时间设置指定日期的毫秒数。Date.prototype.setUTCMinutes()根据通用时间设置指定日期的分钟数。Date.prototype.setUTCMonth()根据世界时间设置指定日期的月份。Date.prototype.setUTCSeconds()根据通用时间设置指定日期的秒数。Date.prototype.setYear()根据当地时间设置指定日期的年份(通常为2-3位数字)。setFullYear()改为使用。

转化getter

Date.prototype.toDateString()Date作为可读的字符串返回“日期”部分。Date.prototype.toISOString()将日期转换为遵循ISO 8601扩展格式的字符串。Date.prototype.toJSON()返回表示Date使用的字符串toISOString()。打算供使用JSON.stringify()Date.prototype.toGMTString()返回表示Date以GMT(UT)时区为基础的字符串。toUTCString()改为使用。Date.prototype.toLocaleDateString()根据系统设置返回一个字符串,其中包含此日期的日期部分的局部性敏感表示。Date.prototype.toLocaleFormat()使用格式字符串将日期转换为字符串。Date.prototype.toLocaleString()返回此日期的区域敏感表示形式的字符串。覆盖该Object.prototype.toLocaleString()方法。Date.prototype.toLocaleTimeString()根据系统设置返回此日期时间部分的局部性敏感表示的字符串。Date.prototype.toSource()返回一个表示等效Date对象来源的字符串; 你可以使用这个值来创建一个新的对象。覆盖该Object.prototype.toSource()方法。Date.prototype.toString()返回表示指定Date对象的字符串。覆盖该Object.prototype.toString()方法。Date.prototype.toTimeString()返回Date作为人可读字符串的“时间”部分。Date.prototype.toUTCString()使用UTC时区将日期转换为字符串。Date.prototype.valueOf()返回Date对象的原始值。覆盖该Object.prototype.valueOf()方法。

规格

Specification

Status

Comment

ECMAScript 1st Edition (ECMA-262)

Standard

Initial definition. Implemented in JavaScript 1.1.

ECMAScript 5.1 (ECMA-262)The definition of 'Date.prototype' in that specification.

Standard

ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Date.prototype' in that specification.

Standard

ECMAScript Latest Draft (ECMA-262)The definition of 'Date.prototype' in that specification.

Draft

浏览器兼容性

Feature

Chrome

Firefox

Edge

Internet Explorer

Opera

Safari

Basic Support

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

Ordinary object (ES2015)

?

41

?

?

?

?

Feature

Android

Chrome for Android

Edge mobile

Firefox for Android

IE mobile

Opera Android

iOS Safari

Basic Support

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

Ordinary object (ES2015)

?

?

?

41

?

?

?

JavaScript

JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络