对象: 日期
Date是javascript预定义主题. 该主题包括可获取或设置系统日期、时间、月、日、小时、分钟、秒等等。包括函数方法获取 UTC 格式的日期。 UTC 是国际标准时间,也称格林尼治标准时间(Greenwich Mean Time)。
在使用这些方法前可先声明一个主题。
创建新日期主题
示例代码:var exd = new Date();
调用"new Date()" 将创建一个日期主题。该主题可用于调用过程或函数。我们必须在变量中获取该主题。在上述示例代码中该主题存储于变量 exd 中。
以下将使用 exd 作为示例。
方法 | 示例代码 | 结果 | 说明 |
getDate() | exd = new Date(); exd.getDate(); | 23 9 | getDate() 方法返回您当前系统日期中的天数。 其范围为 1 到 31。 |
getUTCDate() | exd = new Date(); exd.getUTCDate(); | 23 9 | getUTCDate() 方法返回 UTC 日期中的天数。 UTC 是国际标准时间。 |
getMonth() | exd = new Date(); exd.getMonth(); | 3 3 | getMonth() 方法返回您系统日期中的月份。 其范围是从 1 到 12. 1 是1月, 12 是12月。 |
getUTCMonth() | exd = new Date(); exd. getUTCMonth(); | 3 3 | getUTCMonth() 方法返回您系统日期中的 UTC 时间中的月份。 |
getDay() | exd = new Date(); exd.getDay(); | 5 5 | getDay() 方法返回您系统日期为一周中的星期几。 其返回范围为从 1 到 7. 1 为星期日, 2 是星期一, 到 7 为星期六。 |
getUTCDay() | exd = new Date(); exd.getUTCDay(); | 5 5 | getUTCDay() 方法返回您系统 UTC 日期是一周中的星期几。 |
getHours() | exd = new Date(); exd.getHours(); | 14 20 | getHours() 方法返回您系统日期的小时。 该范围是 0 到 24. |
getUTCHours() | exd = new Date(); exd.getUTCHours(); | 6 12 | getUTCHours() 方法返回您系统 UTC 日期的小时。 [中国时间比国际标准时间(UTC)快 8 小时]. |
getMinutes() | exd = new Date(); exd.getMinutes(); | 2 32 | getMinutes() 方法返回您系统日期的分钟。 其结果范围是 0 到 59。 |
getUTCMinutes() | exd = new Date(); exd. getUTCMinutes(); | 2 32 | getUTCMinutes() 方法返回您系统 UTC 日期的分钟。 |
getSeconds() | exd = new Date(); exd.getSeconds(); | 1 30 | getSeconds() 方法返回您系统日期的分钟。 其结果范围是 0 到 59。 |
getUTCSeconds() | exd = new Date(); exd. getUTCSeconds(); | 1 30 | getUTCSeconds() 方法返回您系统 UTC 日期的秒钟。 |
getMilliseconds() | exd = new Date(); exd.getMilliseconds(); | 562 872 | getMilliseconds() 方法返回您当前系统时钟的毫秒数。 |
getTime() | exd = new Date(); exd.getTime(); | - | 结果: 1272002521562 1270816350872 getTime() 方法返回自 1970 年 1 月 1 日起到当前系统时间的毫秒数。 |
getYear() | exd = new Date(); exd.getYear(); | 2010 110 | getYear() 方法返回自 1900 开始的当前时间的年份。 |
getFullYear() | exd = new Date(); exd.getFullYear(); | 2010 2010 | getFullYear() 方法返回当前系统日期中的完整年份。 |
toGMTString() | exd = new Date(); exd.toGMTString(); | Fri, 23 Apr 2010 06:02:01 UTC Fri, 09 Apr 2010 12:32:30 GMT | toGMTString() 方法返回格林尼治标准时间(GMT)格式的日期、时间。 |
事实上 JavaScript 保存的是自 1970 年 1 月 1 日午夜起至今的毫秒数。 该日期称为纪元日期(Epoch Date)。