📜  JavaScript中的日期

📅  最后修改于: 2020-10-24 05:28:24             🧑  作者: Mango

JavaScript日期对象

JavaScript日期对象可用于获取年,月和日。您可以借助JavaScript日期对象在网页上显示计时器。

您可以使用不同的Date构造函数来创建日期对象。它提供了获取和设置日,月,年,小时,分钟和秒的方法。

构造函数

您可以使用Date构造函数的4个变体创建日期对象。

  • Date()
  • Date(milliseconds)
  • Date(dataString)
  • Date(year, month, day, hours, minutes, seconds, milliseconds)

JavaScript日期方法

让我们看一下JavaScript日期方法及其说明的列表。

Methods Description
getDate() It returns the integer value between 1 and 31 that represents the day for the specified date on the basis of local time.
getDay() It returns the integer value between 0 and 6 that represents the day of the week on the basis of local time.
getFullYears() It returns the integer value that represents the year on the basis of local time.
getHours() It returns the integer value between 0 and 23 that represents the hours on the basis of local time.
getMilliseconds() It returns the integer value between 0 and 999 that represents the milliseconds on the basis of local time.
getMinutes() It returns the integer value between 0 and 59 that represents the minutes on the basis of local time.
getMonth() It returns the integer value between 0 and 11 that represents the month on the basis of local time.
getSeconds() It returns the integer value between 0 and 60 that represents the seconds on the basis of local time.
getUTCDate() It returns the integer value between 1 and 31 that represents the day for the specified date on the basis of universal time.
getUTCDay() It returns the integer value between 0 and 6 that represents the day of the week on the basis of universal time.
getUTCFullYears() It returns the integer value that represents the year on the basis of universal time.
getUTCHours() It returns the integer value between 0 and 23 that represents the hours on the basis of universal time.
getUTCMinutes() It returns the integer value between 0 and 59 that represents the minutes on the basis of universal time.
getUTCMonth() It returns the integer value between 0 and 11 that represents the month on the basis of universal time.
getUTCSeconds() It returns the integer value between 0 and 60 that represents the seconds on the basis of universal time.
setDate() It sets the day value for the specified date on the basis of local time.
setDay() It sets the particular day of the week on the basis of local time.
setFullYears() It sets the year value for the specified date on the basis of local time.
setHours() It sets the hour value for the specified date on the basis of local time.
setMilliseconds() It sets the millisecond value for the specified date on the basis of local time.
setMinutes() It sets the minute value for the specified date on the basis of local time.
setMonth() It sets the month value for the specified date on the basis of local time.
setSeconds() It sets the second value for the specified date on the basis of local time.
setUTCDate() It sets the day value for the specified date on the basis of universal time.
setUTCDay() It sets the particular day of the week on the basis of universal time.
setUTCFullYears() It sets the year value for the specified date on the basis of universal time.
setUTCHours() It sets the hour value for the specified date on the basis of universal time.
setUTCMilliseconds() It sets the millisecond value for the specified date on the basis of universal time.
setUTCMinutes() It sets the minute value for the specified date on the basis of universal time.
setUTCMonth() It sets the month value for the specified date on the basis of universal time.
setUTCSeconds() It sets the second value for the specified date on the basis of universal time.
toDateString() It returns the date portion of a Date object.
toISOString() It returns the date in the form ISO format string.
toJSON() It returns a string representing the Date object. It also serializes the Date object during JSON serialization.
toString() It returns the date in the form of string.
toTimeString() It returns the time portion of a Date object.
toUTCString() It converts the specified date in the form of string using UTC time zone.
valueOf() It returns the primitive value of a Date object.

JavaScript日期示例

让我们看一下print日期对象的简单示例。它同时打印日期和时间。

Current Date and Time: 

输出:

Current Date and Time: Thu Oct 22 2020 18:16:27 GMT+0800 (China Standard Time)  

让我们看看另一个print日期/月份/年份的代码。


输出:

Date is: 22/10/2020  

JavaScript当前时间示例

让我们看一下print系统当前时间的简单示例。

Current Time: 

输出:

Current Time: 18:16:27

JavaScript数字时钟示例

让我们看一个使用JavaScript日期对象显示数字时钟的简单示例。

在JavaScript中有两种设置间隔的方法:通过setTimeout()或setInterval()方法。

Current Time: 

输出:

Current Time: