JavaScript手册 
目录
此参考中包含
的内容
轻松上手
简介
操作符
语句
核心
文档
窗口
表单
浏览器
事件和
事件句柄
LiveWire
数据库服务
进程管理服务
实用工具
全局函数
LiveConnect
的Java包
索引
版权
 
【目录】 【上一页】 【下一页】 【索引】

Date

Lets you work with dates and times.

Core object
实现版本Navigator 2.0, LiveWire 1.0
Navigator 3.0: 添加了 prototype 属性

创建源

The Date constructor:

new Date()
new Date("month day, year hours:minutes:seconds")
new Date(yr_num, mo_num, day_num)
new Date(yr_num, mo_num, day_num, hr_num, min_num, sec_num)

参数

month, day, year,
hours, minutes,
seconds
String values representing part of a date.
yr_num, mo_num,
day_num, hr_num,
min_num, sec_num
Integer values representing part of a date. As an integer value, the month is represented by 0 to 11 with 0=January and 11=December.

描述

If you supply no arguments, the constructor creates a Date object for today's date and 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, and seconds.

The way JavaScript handles dates is very similar to the way Java handles dates: both languages have many of the same date methods, and both store dates internally as the number of milliseconds since January 1, 1970 00:00:00. Dates prior to 1970 are not allowed.

属性概览

prototypeAllows the addition of properties to a Date object.

方法概览

getDateReturns the day of the month for the specified date.
getDayReturns the day of the week for the specified date.
getHoursReturns the hour in the specified date.
getMinutesReturns the minutes in the specified date.
getMonthReturns the month in the specified date.
getSecondsReturns the seconds in the specified date.
getTimeReturns the numeric value corresponding to the time for the specified date.
getTimezoneOffsetReturns the time-zone offset in minutes for the current locale.
getYearReturns the year in the specified date.
parseReturns the number of milliseconds in a date string since January 1, 1970, 00:00:00, local time.
setDateSets the day of the month for a specified date.
setHoursSets the hours for a specified date.
setMinutesSets the minutes for a specified date.
setMonthSets the month for a specified date.
setSecondsSets the seconds for a specified date.
setTimeSets the value of a Date object.
setYearSets the year for a specified date.
toGMTStringConverts a date to a string, using the Internet GMT conventions.
toLocaleStringConverts a date to a string, using the current locale's conventions.
UTCReturns the number of milliseconds in a Date object since January 1, 1970, 00:00:00, Universal Coordinated Time (GMT).

示例

The following示例 show several ways to assign dates:

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

属性

prototype

Represents the prototype for this class. You can use the prototype to add properties or methods to all instances of a class. For information on prototypes, see Function.prototype.

属性源Date
实现版本Navigator 3.0, LiveWire 1.0

方法

getDate

Returns the day of the month for the specified date.

方法源Date
实现版本Navigator 2.0, LiveWire 1.0

语法

getDate()

参数

描述

The value returned by getDate is an integer between 1 and 31.

示例

The second statement below assigns the value 25 to the variable day, based on the value of the Date object Xmas95.

Xmas95 = new Date("December 25, 1995 23:15:00")
day = Xmas95.getDate()

参看

Date.setDate

getDay

Returns the day of the week for the specified date.

方法源Date
实现版本Navigator 2.0, LiveWire 1.0

语法

getDay()

参数

描述

The value returned by getDay is an integer corresponding to the day of the week: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.

示例

The second statement below assigns the value 1 to weekday, based on the value of the Date object Xmas95. December 25, 1995, is a Monday.

Xmas95 = new Date("December 25, 1995 23:15:00")
weekday = Xmas95.getDay()

getHours

Returns the hour for the specified date.

方法源Date
实现版本Navigator 2.0, LiveWire 1.0

语法

getHours()

参数

描述

The value returned by getHours is an integer between 0 and 23.

示例

The second statement below assigns the value 23 to the variable hours, based on the value of the Date object Xmas95.

Xmas95 = new Date("December 25, 1995 23:15:00")
hours = Xmas95.getHours()

参看

Date.setHours

getMinutes

Returns the minutes in the specified date.

方法源Date
实现版本Navigator 2.0, LiveWire 1.0

语法

getMinutes()

参数

描述

The value returned by getMinutes is an integer between 0 and 59.

示例

The second statement below assigns the value 15 to the variable minutes, based on the value of the Date object Xmas95.

Xmas95 = new Date("December 25, 1995 23:15:00")
minutes = Xmas95.getMinutes()

参看

Date.setMinutes

getMonth

Returns the month in the specified date.

方法源Date
实现版本Navigator 2.0, LiveWire 1.0

语法

getMonth()

参数

描述

The value returned by getMonth is an integer between 0 and 11. 0 corresponds to January, 1 to February, and so on.

示例

The second statement below assigns the value 11 to the variable month, based on the value of the Date object Xmas95.

Xmas95 = new Date("December 25, 1995 23:15:00")
month = Xmas95.getMonth()

参看

Date.setMonth

getSeconds

Returns the seconds in the current time.

方法源Date
实现版本Navigator 2.0, LiveWire 1.0

语法

getSeconds()

参数

描述

The value returned by getSeconds is an integer between 0 and 59.

示例

The second statement below assigns the value 30 to the variable secs, based on the value of the Date object Xmas95.

Xmas95 = new Date("December 25, 1995 23:15:30")
secs = Xmas95.getSeconds()

参看

Date.setSeconds

getTime

Returns the numeric value corresponding to the time for the specified date.

方法源Date
实现版本Navigator 2.0, LiveWire 1.0

语法

getTime()

参数

描述

The value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00. You can use this method to help assign a date and time to another Date object.

示例

The following example assigns the date value of theBigDay to sameAsBigDay:

theBigDay = new Date("July 1, 1999")
sameAsBigDay = new Date()
sameAsBigDay.setTime(theBigDay.getTime())

参看

Date.setTime

getTimezoneOffset

Returns the time-zone offset in minutes for the current locale.

方法源Date
实现版本Navigator 2.0, LiveWire 1.0

语法

getTimezoneOffset()

参数

描述

The time-zone offset is the difference between local time and Greenwich Mean Time (GMT). Daylight savings time prevents this value from being a constant.

示例

x = new Date()
currentTimeZoneOffsetInHours = x.getTimezoneOffset()/60

getYear

Returns the year in the specified date.

方法源Date
实现版本Navigator 2.0, LiveWire 1.0

语法

getYear()

参数

描述

The getYear method returns either a 2-digit or 4-digit year:

示例

示例 1. The second statement assigns the value 95 to the variable year.

Xmas = new Date("December 25, 1995 23:15:00")
year = Xmas.getYear()
示例 2. The second statement assigns the value 2000 to the variable year.

Xmas = new Date("December 25, 2000 23:15:00")
year = Xmas.getYear()
示例 3. The second statement assigns the value 95 to the variable year, representing the year 1995.

Xmas.setYear(95)
year = Xmas.getYear()

参看

Date.setYear

parse

Returns the number of milliseconds in a date string since January 1, 1970, 00:00:00, local time.

方法源Date
静态
实现版本Navigator 2.0, LiveWire 1.0

语法

Date.parse(dateString)

参数

:

dateStringA string representing a date.

描述

The parse method takes a date string (such as "Dec 25, 1995") and returns the number of milliseconds since January 1, 1970, 00:00:00 (local time). This function is useful for setting date values based on string values, for example in conjunction with the setTime method and the Date object.

Given a string representing a time, parse returns the time value. It accepts the IETF standard date语法: "Mon, 25 Dec 1995 13:30:00 GMT". It understands the continental US time-zone abbreviations, but for general use, use a time-zone offset, for example, "Mon, 25 Dec 1995 13:30:00 GMT+0430" (4 hours, 30 minutes west of the Greenwich meridian). If you do not specify a time zone, the local time zone is assumed. GMT and UTC are considered equivalent.

Because parse is a static method of Date, you always use it as Date.parse(), rather than as a method of a Date object you created.

示例

If IPOdate is an existing Date object, then you can set it to August 9, 1995 as follows:

IPOdate.setTime(Date.parse("Aug 9, 1995"))

参看

Date.UTC

setDate

Sets the day of the month for a specified date.

方法源Date
实现版本Navigator 2.0, LiveWire 1.0

语法

setDate(dayValue)

参数

dayValueAn integer from 1 to 31, representing the day of the month.

示例

The second statement below changes the day for theBigDay to July 24 from its original value.

theBigDay = new Date("July 27, 1962 23:30:00")
theBigDay.setDate(24)

参看

Date.getDate

setHours

Sets the hours for a specified date.

方法源Date
实现版本Navigator 2.0, LiveWire 1.0

语法

setHours(hoursValue)

参数

hoursValueAn integer between 0 and 23, representing the hour.

示例

theBigDay.setHours(7)

参看

Date.getHours

setMinutes

Sets the minutes for a specified date.

方法源Date
实现版本Navigator 2.0, LiveWire 1.0

语法

setMinutes(minutesValue)

参数

minutesValueAn integer between 0 and 59, representing the minutes.

示例

theBigDay.setMinutes(45)

参看

Date.getMinutes

setMonth

Sets the month for a specified date.

方法源Date
实现版本Navigator 2.0, LiveWire 1.0

语法

setMonth(monthValue)

参数

monthValueAn integer between 0 and 11 (representing the months January through December).

示例

theBigDay.setMonth(6)

参看

Date.getMonth

setSeconds

Sets the seconds for a specified date.

方法源Date
实现版本Navigator 2.0, LiveWire 1.0

语法

setSeconds(secondsValue)

参数

secondsValueAn integer between 0 and 59.

示例

theBigDay.setSeconds(30)

参看

Date.getSeconds

setTime

Sets the value of a Date object.

方法源Date
实现版本Navigator 2.0, LiveWire 1.0

语法

setTime(timevalue)

参数

timevalueAn integer representing the number of milliseconds since 1 January 1970 00:00:00.

描述

Use the setTime method to help assign a date and time to another Date object.

示例

theBigDay = new Date("July 1, 1999")
sameAsBigDay = new Date()
sameAsBigDay.setTime(theBigDay.getTime())

参看

Date.getTime

setYear

Sets the year for a specified date.

方法源Date
实现版本Navigator 2.0, LiveWire 1.0

语法

setYear(yearValue)

参数

yearValueAn integer.

描述

If yearValue is a number between 0 and 99 (inclusive), then the year for dateObjectName is set to 1900 + yearValue. Otherwise, the year for dateObjectName is set to yearValue.

示例

Note that there are two ways to set years in the 20th century.

示例 1. The year is set to 1996.

theBigDay.setYear(96) 示例 2. The year is set to 1996.

theBigDay.setYear(1996) 示例 3. The year is set to 2000.

theBigDay.setYear(2000)

参看

Date.getYear

toGMTString

Converts a date to a string, using the Internet GMT conventions.

方法源Date
实现版本Navigator 2.0, LiveWire 1.0

语法

toGMTString()

参数

描述

The exact format of the value returned by toGMTString varies according to the platform.

示例

In the following example, today is a Date object:

today.toGMTString() In this example, the toGMTString method converts the date to GMT (UTC) using the operating system's time-zone offset and returns a string value that is similar to the following form. The exact format depends on the platform.

Mon, 18 Dec 1995 17:28:35 GMT

参看

Date.toLocaleString

toLocaleString

Converts a date to a string, using the current locale's conventions.

方法源Date
实现版本Navigator 2.0, LiveWire 1.0

语法

toLocaleString()

参数

描述

If you pass a date using toLocaleString, be aware that different platforms assemble the string in different ways. Using methods such as getHours, getMinutes, and getSeconds gives more por表 results.

示例

In the following example, today is a Date object:

today = new Date(95,11,18,17,28,35) //months are represented by 0 to 11
today.toLocaleString()
In this example, toLocaleString returns a string value that is similar to the following form. The exact format depends on the platform.

12/18/95 17:28:35

参看

Date.toGMTString

UTC

Returns the number of milliseconds in a Date object since January 1, 1970, 00:00:00, Universal Coordinated Time (GMT).

方法源Date
静态
实现版本Navigator 2.0, LiveWire 1.0

语法

Date.UTC(year, month, day, hrs, min, sec)

参数

yearA year after 1900.
monthA month between 0 and 11.
dateA day of the month between 1 and 31.
hrs(Optional) A number of hours between 0 and 23.
min(Optional) A number of minutes between 0 and 59.
sec(Optional) A number of seconds between 0 and 59.

描述

UTC takes comma-delimited date parameters and returns the number of milliseconds since January 1, 1970, 00:00:00, Universal Coordinated Time (GMT).

Because UTC is a static method of Date, you always use it as Date.UTC(), rather than as a method of a Date object you created.

示例

The following statement creates a Date object using GMT instead of local time:

gmtDate = new Date(Date.UTC(96, 11, 1, 0, 0, 0))

参看

Date.parse


【目录】 【上一页】 【下一页】 【索引】

回页面顶部回页面顶部