📜  JavaScript-日期对象

📅  最后修改于: 2020-12-18 05:03:30             🧑  作者: Mango


Date对象是JavaScript语言内置的数据类型。日期对象使用新的Date()创建,如下所示。

创建Date对象后,可以使用多种方法对其进行操作。大多数方法仅允许您使用本地时间或UTC(通用或GMT)时间来获取和设置对象的年,月,日,时,分,秒和毫秒字段。

ECMAScript标准要求Date对象必须能够在1970年1月1日之前或之后的1亿天内以毫秒精度表示任何日期和时间。范围是正负273,785年,因此JavaScript可以表示直到275755年的日期和时间。

句法

您可以使用以下任何语法使用Date()构造函数创建Date对象。

new Date( )
new Date(milliseconds)
new Date(datestring)
new Date(year,month,date[,hour,minute,second,millisecond ])

–括号中的参数始终是可选的。

这是参数的描述-

  • 无参数-无参数,Date()构造函数创建一个Date对象,该对象设置为当前日期和时间。

  • 毫秒-传递一个数字参数时,它将作为日期的内部数字表示,以毫秒为单位,由getTime()方法返回。例如,传递参数5000将创建一个表示1/1/70午夜之后五秒的日期。

  • datestring-传递一个字符串参数时,它是日期的字符串表示形式,其格式由Date.parse()方法接受。

  • 7个要点-要使用上面显示的构造函数的最后一种形式。这是每个参数的描述-

    • -代表年份的整数值。为了兼容性(为了避免Y2K问题),应始终指定完整的年份;使用1998,而不是98。

    • month-代表月份的整数值,从1月的0到12月的11开始。

    • 日期-表示该月的一天整数值。

    • 小时-代表一天(24小时刻度)中的小时的整数值。

    • 分钟-表示时间读数的分钟段的整数值。

    • 第二-表示时间阅读的第二段的整数值。

    • 毫秒-表示时间读数的毫秒段的整数值。

日期属性

这是Date对象的属性及其描述的列表。

Sr.No. Property & Description
1 constructor

Specifies the function that creates an object’s prototype.

2 prototype

The prototype property allows you to add properties and methods to an object

在以下各节中,我们将通过一些示例来演示不同Date属性的用法。

日期方法

这是与Date一起使用的方法及其说明的列表。

Sr.No. Method & Description
1 Date()

Returns today’s date and time

2 getDate()

Returns the day of the month for the specified date according to local time.

3 getDay()

Returns the day of the week for the specified date according to local time.

4 getFullYear()

Returns the year of the specified date according to local time.

5 getHours()

Returns the hour in the specified date according to local time.

6 getMilliseconds()

Returns the milliseconds in the specified date according to local time.

7 getMinutes()

Returns the minutes in the specified date according to local time.

8 getMonth()

Returns the month in the specified date according to local time.

9 getSeconds()

Returns the seconds in the specified date according to local time.

10 getTime()

Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC.

11 getTimezoneOffset()

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

12 getUTCDate()

Returns the day (date) of the month in the specified date according to universal time.

13 getUTCDay()

Returns the day of the week in the specified date according to universal time.

14 getUTCFullYear()

Returns the year in the specified date according to universal time.

15 getUTCHours()

Returns the hours in the specified date according to universal time.

16 getUTCMilliseconds()

Returns the milliseconds in the specified date according to universal time.

17 getUTCMinutes()

Returns the minutes in the specified date according to universal time.

18 getUTCMonth()

Returns the month in the specified date according to universal time.

19 getUTCSeconds()

Returns the seconds in the specified date according to universal time.

20 getYear()

Deprecated – Returns the year in the specified date according to local time. Use getFullYear instead.

21 setDate()

Sets the day of the month for a specified date according to local time.

22 setFullYear()

Sets the full year for a specified date according to local time.

23 setHours()

Sets the hours for a specified date according to local time.

24 setMilliseconds()

Sets the milliseconds for a specified date according to local time.

25 setMinutes()

Sets the minutes for a specified date according to local time.

26 setMonth()

Sets the month for a specified date according to local time.

27 setSeconds()

Sets the seconds for a specified date according to local time.

28 setTime()

Sets the Date object to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC.

29 setUTCDate()

Sets the day of the month for a specified date according to universal time.

30 setUTCFullYear()

Sets the full year for a specified date according to universal time.

31 setUTCHours()

Sets the hour for a specified date according to universal time.

32 setUTCMilliseconds()

Sets the milliseconds for a specified date according to universal time.

33 setUTCMinutes()

Sets the minutes for a specified date according to universal time.

34 setUTCMonth()

Sets the month for a specified date according to universal time.

35 setUTCSeconds()

Sets the seconds for a specified date according to universal time.

36 setYear()

Deprecated – Sets the year for a specified date according to local time. Use setFullYear instead.

37 toDateString()

Returns the “date” portion of the Date as a human-readable string.

38 toGMTString()

Deprecated – Converts a date to a string, using the Internet GMT conventions. Use toUTCString instead.

39 toLocaleDateString()

Returns the “date” portion of the Date as a string, using the current locale’s conventions.

40 toLocaleFormat()

Converts a date to a string, using a format string.

41 toLocaleString()

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

42 toLocaleTimeString()

Returns the “time” portion of the Date as a string, using the current locale’s conventions.

43 toSource()

Returns a string representing the source for an equivalent Date object; you can use this value to create a new object.

44 toString()

Returns a string representing the specified Date object.

45 toTimeString()

Returns the “time” portion of the Date as a human-readable string.

46 toUTCString()

Converts a date to a string, using the universal time convention.

47 valueOf()

Returns the primitive value of a Date object.

使用通用时间约定将日期转换为字符串。

日期静态方法

除了前面列出的许多实例方法外,Date对象还定义了两个静态方法。这些方法是通过Date()构造函数本身调用的。

Sr.No. Method & Description
1 Date.parse( )

Parses a string representation of a date and time and returns the internal millisecond representation of that date.

2 Date.UTC( )

Returns the millisecond representation of the specified UTC date and time.

在以下各节中,我们将通过一些示例来演示Date Static方法的用法。