Javascript 简明教程

JavaScript - Date

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

The Date object is a datatype built into the JavaScript language. Date objects are created with the new Date( ) as shown below.

创建日期对象后,可以使用许多方法对其进行操作。大多数方法只允许你使用本地时间或 UTC(全局时间或格林尼治标准时间)时间来获取和设置对象的年份、月份、日期、小时、分钟、秒和毫秒字段。

Once a Date object is created, a number of methods allow you to operate on it. Most methods simply allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object using either local time or UTC (universal, or GMT) time.

ECMAScript 标准要求“日期”对象在 1/1/1970 之前或之后的 1 亿天内能够以毫秒精度表示任何日期和时间。这是一个加减 273,785 年的范围,因此 JavaScript 可以表示直到公元 275755 年的日期和时间。

The ECMAScript standard requires the Date object to be able to represent any date and time, to millisecond precision, within 100 million days before or after 1/1/1970. This is a range of plus or minus 273,785 years, so JavaScript can represent the date and time till the year 275755.

Syntax

你可以使用以下任何一种语法,使用 Date() 构造函数来创建一个 Date 对象:

You can use any of the following syntaxes to create a Date object using the Date() constructor −

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

Note − 在方括号中的参数始终是可选的。

Note − Parameters in the brackets are always optional.

Parameters

  1. No Argument − With no arguments, the Date() constructor creates a Date object set to the current date and time.

  2. milliseconds − When one numeric argument is passed, it is taken as the internal numeric representation of the date in milliseconds, as returned by the getTime() method. For example, passing the argument 5000 creates a date that represents five seconds past midnight on 1/1/70.

  3. datestring − When one string argument is passed, it is a string representation of a date in the format accepted by the Date.parse() method.

  4. 7 agruments − To use the last form of the constructor shown above. Here is a description of each argument − year − Integer value representing the year. For compatibility (in order to avoid the Y2K problem), you should always specify the year in full; use 1998 rather than 98.month − Integer value representing the month, beginning with 0 for January to 11 for December.date − Integer value representing the day of the month.hour − Integer value representing the hour of the day (24-hour scale).minute − Integer value representing the minute segment of a time reading.second − Integer value representing the second segment of a time reading.millisecond − Integer value representing the millisecond segment of a time reading.

Return Value

它返回包含日期、月、日、年、小时、分、秒和时区的日期字符串,如下所示。

It returns the date string containing day, month, date, year, hours, minutes, seconds, and timezone, as shown below.

Wed Aug 09 2023 09:24:03 GMT+0530 (India Standard Time)

JavaScript Date Reference

在 JavaScript 中,Date 对象提供用于创建、处理和格式化日期和时间的方法。在这里,我们列举了 Date 类中所有现存的方法 −

In JavaScript, the Date object provides methods for creating, manipulating, and formatting dates and times. Here, we have listed all the methods present in Date class −

JavaScript Date Methods

这里列举了与 Date 一起使用的各种方法及其说明。

Here is a list of the methods used with Date and their description.

Date Static Methods

我们使用 Date 对象调用这些方法 −

These methods are invoked using the Date object −

Sr.No.

Name & 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 Methods

我们使用 Date 对象的实例调用这些方法 −

These methods are invoked using the instance of the Date object −

Sr.No.

Name & Description

1

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

2

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

3

getFullYear()Returns the year of the specified date according to local time.

4

getHours()Returns the hour in the specified date according to local time.

5

getMilliseconds()Returns the milliseconds in the specified date according to local time.

6

getMinutes()Returns the minutes in the specified date according to local time.

7

getMonth()Returns the month in the specified date according to local time.

8

getSeconds()Returns the seconds in the specified date according to local time.

9

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

10

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

11

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

12

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

13

getUTCFullYear()Returns the year in the specified date according to universal time.

14

getUTCHours()Returns the hours in the specified date according to universal time.

15

getUTCMilliseconds()Returns the milliseconds in the specified date according to universal time.

16

getUTCMinutes()Returns the minutes in the specified date according to universal time.

17

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

18

getUTCSeconds()Returns the seconds in the specified date according to universal time.

19

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

20

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

21

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

22

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

23

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

24

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

25

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

26

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

27

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

28

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

29

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

30

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

31

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

32

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

33

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

34

toDateString()Returns the "date" portion of the Date as a human-readable string.

35

toISOString()Returns date string in the ISO format.

36

toLocaleDateString()Returns the "date" portion of the Date as a string, using the current locale’s conventions.

37

toJSON()Converts a date to a string.

38

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

39

toLocaleTimeString()Returns the "time" portion of the Date as a string, using the current locale’s conventions.

40

toString()Returns a string representing the specified Date object.

41

toTimeString()Returns the "time" portion of the Date as a human-readable string.

42

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

43

valueOf()Returns the primitive value of a Date object.

Sr.No.

Name & Description

1

Date()Returns today’s date and time.

Example: Creating JavaScript Date Object

在下面的示例中,我们创建了日期对象的新实例。在输出中,你可以看到它返回当前时间。

In the example below, we create a new instance of the date object. In the output, you can observe that it returns the current time.

<html>
<head>
   <title> JavaScript - Date object </title>
</head>
<body>
<p id = "output"> </p>
<script>
   const date = new Date();
   document.getElementById("output").innerHTML =
      "Today's date is : " + date;
</script>
</body>
</html>

Output

如果执行以上程序,它将返回当前时间。

If we execute the above program, it returns the current time.

Example: Setting up custom date

在下面的示例中,我们已将自定义日期字符串作为 Date() 构造函数的参数传递,以创建自定义日期。

In the example below, We have passed the custom date string as a parameter of the Date() constructor to create a custom date.

Date() 构造函数返回标准日期字符串,你可以在输出中看到。

The Date() constructor returns the standard date string, which you can see in the output.

<html>
<head>
    <title> JavaScript - Date object </title>
</head>
<body>
<p id = "output"> </p>
<script>
   const date = new Date("August 19, 2024 09:30:54");
   document.getElementById("output").innerHTML =
      "The custom date is : " + date;
</script>
</body>
</html>

Output

如果执行以上程序,它将返回提供的自定义时间。

If we execute the above program, it returns the custom time as provided.

Example

在下面的示例中,我们已将毫秒作为 Date() 构造函数的参数进行传递。如果你传递正毫秒作为参数,该对象将根据 1970 年 1 月 1 日 00:00:00 + 毫秒返回日期。

In the example below, we have passed milliseconds as an argument of the Date() constructor. If you pass the positive milliseconds as an argument, the object returns the date according to the 1st January 1970 00:00:00 + milliseconds.

如果将负毫秒作为参数传递,它将根据 1970 年 1 月 1 日 00:00:00 – 毫秒返回日期。

Otherwise, it returns the date according to the 1st January 1970 00:00:00 – milliseconds if negative milliseconds passed as an argument.

<html>
<head>
   <title> JavaScript - Date object </title>
</head>
<body>
<p id = "output"> </p>
<script>
   const output = document.getElementById("output");
   let date = new Date(999999999999);
   output.innerHTML += "The Date after 1st January, 1970 is - " + date + "<br>";
   date = new Date(-999999999999);
   output.innerHTML += "The Date before 1st January, 1970 is - " + date;
</script>
</body>
</html>

Output

它返回 1970 年 1 月 1 日之后和 1970 年 1 月 1 日之前的结果。

It returns the date after 1st January, 1970 and before 1st January, 1970 as result.

Example: Constructing a date from 7 arguments

在下面的示例中,我们已将年、月、日、时、分、秒和毫秒作为 Date() 构造函数的参数传递。Date() 构造函数返回完整的日期字符串,你可以在输出中看到。

In the example below, we have passed the year, month, date, hour, minute, second, and millisecond as a Date() constructor argument. The Date() constructor returns the full date string, which you can see in the output.

<html>
<head>
   <title> JavaScript - Date object </title>
</head>
<body>
<p id = "output"> </p>
<script>
   const date = new Date(2001, 5, 14, 6, 43, 58, 342);
   document.getElementById("output").innerHTML =
      "The custom date is : " + date;
</script>
</body>
</html>

Output

如果执行以上程序,它将返回提供的自定义时间。

If we execute the above program, it returns the custom time as provided.

但是,可以使用日期对象的各种方法来格式化日期字符串。我们来看下面的示例。

However, you can use the different methods of the Date object to format the date string. Let’s look at the example below.

Example: Formatting a date string

在下面的示例中,使用了三种不同的方法来格式化日期字符串。

In the example below, three different methods are used to format the date string.

toDateString() 方法从日期字符串中仅提取日期,并删除时间部分。

The toDateString() method extracts the date only from the date string and removes the time part.

toISOString() 方法将日期字符串转换为 ISO 格式。

The toISOString() method converts the date string into the ISO format.

toUTCString() 方法将日期字符串转换为 UTC 时间格式。

The toUTCString() method converts the date string into the UTC time format.

<html>
<head>
   <title> JavaScript - Formatting the date </title>
</head>
<body>
<p id = "output"> </p>
<script>
   const date = new Date(999999999999);
   document.getElementById("output").innerHTML +=
      "The Date after 1st January, 1970 is: " + date.toDateString() + "<br>"+
      "The Date after 1st January, 1970 is: " + date.toISOString() + "<br>"+
      "The Date after 1st January, 1970 is: " + date.toUTCString();
</script>
</body>
</html>

Output

它将分别返回上述所提供方法的输出。

It will return the ouptut of the above provided methods, respectively.