Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:Date

From MDC

目录

摘要

创建Date 实例用来处理日期和时间。

语法

new Date()
new Date(毫秒)
new Date(日期字符串)
new Date(, , [, , , , 毫秒 ])

参数

毫秒 
Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC.
日期字符串 
String value representing a date. The string should be in a format recognized by the parse method.
代表年份的整数值。为了避免2000年问题最好指定4位数的年份; 使用 1998, 而不要用 98.
代表月份的整数值从0(1月)到11(12月)。
date
代表一个月中的第几天的整数值,从1开始。
hour
代表一天中的小时数的整数值 (24小时制)。
minute
分钟数。
second
秒数。
millisecond
Integer value representing the millisecond segment of a time reading.

描述

JavaScript 1.8 note

For security reasons, you cannot redeclare this global object starting with JavaScript 1.8.


如果没有指定参数, the constructor creates a Date object for today's date and time according to local time. If you supply some arguments but not others, the missing arguments are set to 0. If you supply any arguments, you must supply at least the year, month, and day. You can omit the hours, minutes, seconds, and milliseconds.

The date is measured in milliseconds since midnight 01 January, 1970 UTC. A day holds 86,400,000 milliseconds. The Date object range is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC.

The Date object provides uniform behavior across platforms.

The Date object supports a number of UTC (universal) methods, as well as local time methods. UTC, also known as Greenwich Mean Time (GMT), refers to the time as set by the World Time Standard. The local time is the time known to the computer where JavaScript is executed.

Invoking Date in a non-constructor context (i.e., without the new operator) will return a string representing the current time.

属性

For properties inherited by Date instances, see Properties of Date instances.

prototype
允许为Date增加额外的属性。

Properties inherited from Function.prototype
caller, constructor, length, name

Methods

For methods inherited by Date instances, see Methods of Date instances.

now
Returns the numeric value corresponding to the current time.
parse
Parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00, local time.
UTC
Accepts the same parameters as the longest form of the constructor, and returns the number of milliseconds in a Date object since January 1, 1970, 00:00:00, universal time.

Methods inherited from Function.prototype
apply, call, toSource, toString, valueOf

Methods inherited from Object.prototype
__defineGetter__, __defineSetter__, hasOwnProperty, isPrototypeOf, __lookupGetter__, __lookupSetter__, __noSuchMethod__, propertyIsEnumerable, unwatch, watch

Date instances

Core JavaScript 1.5 Reference:Global Objects:Date:prototype

例子

Example: Several ways to assign dates

The following examples show several ways to assign dates:

today = new Date();
birthday = new Date("December 17, 1995 03:24:00");
birthday = new Date(1995,11,17);
birthday = new Date(1995,11,17,3,24,0);

Example: Calculating elapsed time

The following examples show how to determine the elapsed time between two dates:

// using static methods
var start = Date.now();
// the event you'd like to time goes here:
doSomethingForALongTime();
var end = Date.now();
var elapsed = end - start; // time in milliseconds
// if you have Date objects
var start = new Date();
// the event you'd like to time goes here:
doSomethingForALongTime();
var end = new Date();
var elapsed = end.getTime() - start.getTime(); // time in milliseconds