English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript基础教程

JavaScript 对象

JavaScript 函数

JS HTML DOM

JS 浏览器BOM

AJAX 基础教程

JavaScript 参考手册

JavaScript Dates (Date)

JavaScript的日期和时间

JavaScript日期对象使我们可以处理日期。

在JavaScript中,您可能想创建一个带有日历,火车时刻表或用于设置约会的界面的网站。

这些应用程序需要根据用户的当前时区显示对应时间。

Tue Aug 11 2020 22:34:40 GMT+0800 (中国标准时间)

日期对象

Date对象是在JavaScript内置的对象存储日期和时间。

它提供了许多用于格式化和管理数据的内置方法。

日期对象是使用new Date()构造函数创建的。

let now = new Date();// 将变量设置为当前日期和时间
Test See‹/›

默认情况下,JavaScript将使用浏览器的时区并将日期显示为全文字符串。

创建日期对象

有四种创建新日期对象的方法。

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

  new Date();
  new Date(milliseconds);
  new Date(dateString);
  new Date(year, month, day, hours, minutes, seconds, milliseconds);

new Date()

在new Date()创建与当前日期和时间新的日期对象:

let d = new Date();
Test See‹/›

new Date(milliseconds)

new Date(milliseconds) 会创建一个新的日期对象,该对象为零时间加上毫秒:

let d = new Date(0);
Test See‹/›

JavaScript根据从Unix时间得出的时间戳来理解日期,该时间戳由1970 years1Month1日午夜以来经过的毫秒数组成。

1970 years1Month1日加上252 4600000000毫秒大约是2050 years1Month1日:

let d = new Date(2524600000000);
Test See‹/›

new Date(dateString)

new Date(dateString) 创建了一个从日期字符串一个新的日期对象:

let d = new Date("July 30 1992 18:30");
Test See‹/›

new Date(year, month, ...)

将创建一个具有指定日期和时间新的日期对象:new Date(year, month, ...)

7个数字指定年,月,日,小时,分钟,秒和毫秒(按此顺序):

let d = new Date(2019, 0, 26, 10, 40, 30, 0);
Test See‹/›

在上面的示例中,我们的秒和毫秒设置为30和0。

如果创建日期缺少任何数字,则默认为0。

但是,顺序不能更改,因此,如果您决定省略一个数字,请记住这一点。

您可能还会注意到,一月的月份用0表示,而不是通常的1.

这是因为日期和时间数字从0开始,就像编程中的大多数计数一样。

JavaScript的月份从0到11。一月是0,十二月是11.

日期方法

创建Date对象时,可以使用多种方法对其进行操作。

使用日期方法,您可以使用本地时间或UTC(通用或GMT)时间来获取设置日期,年,月,日,时,分,秒和毫秒。

检索(获取)日期

有了对象日期后,就可以使用各种内置方法访问日期的所有组件。

这些方法将返回相对于当地时区的日期的每个部分。

这些方法中的每一个都以get开头,并将返回相对编号。

The following table is a list of Date objectgetThe table of methods:

MethodDate/Time范围(Range)
getFullYear()Year根据本地时间返回指定日期对象的年份(四位数年份时返回四位数字:YYYY)。
getMonth()Month根据本地时间返回指定日期对象的月份(0-11),(0=一月)。
getDate()第几天(当月)根据本地时间返回指定日期对象的月份中的第几天(1-31).
getDay()一周中的第几天根据本地时间返回指定日期对象的一周中的第几天(0-6),(0 =星期日)。
getHours()Hours根据本地时间返回指定日期对象的小时(0-23).
getMinutes()Minutes根据本地时间返回指定日期对象的分钟(0-59).
getSeconds()Seconds根据本地时间返回指定日期对象的秒数(0-59).
getMilliseconds()Milliseconds根据本地时间返回指定日期对象的毫秒(0-999).
getTime()时间戳

返回从1970-1-1 00:00:00 UTC(协调世界时)到该日期经过的毫秒数

对于1970-1-1 00:00:00 UTC之前的时间返回负值。

getFullYear()方法

getFullYear()方法以四位数的数字返回日期的年份。

var d = new Date();
d.getFullYear();
Test See‹/›

getMonth()方法

getMonth()方法以数字(0到11)返回一年中的月份。

var d = new Date();
d.getMonth();
Test See‹/›

JavaScript的月份从0到11。一月是0,十二月是11.

在以下示例中,month变量保存月份的名称:

var arr = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var d = new Date();
var month = arr[d.getMonth()];
Test See‹/›

getDate()方法

getDate()方法以数字(1-31)形式返回月份中的某天。

var d = new Date();
d.getDate();
Test See‹/›

getDay()方法

getDay()方法以数字(0-6)返回星期几。

var d = new Date();
d.getDay();
Test See‹/›

JavaScript将工作日从0到6进行计数。星期日为0,星期六为6.

在下面的示例中,Today变量保存日期名称:

var arr = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
var d = new Date();
var today = arr[d.getDay()];
Test See‹/›

getHours()方法

getHours()方法将日期的小时数返回为数字(0-23).

var d = new Date();
d.getHours();
Test See‹/›

getMinutes()方法

getMinutes()方法将日期的分钟数返回为数字(0-59).

var d = new Date();
d.getMinutes();
Test See‹/›

getSeconds()方法

getSeconds()方法将日期的秒数返回为数字(0-59).

var d = new Date();
d.getSeconds();
Test See‹/›

getMilliseconds()方法

getMilliseconds()方法以数字(0-999)的形式返回日期的毫秒数。

var d = new Date();
d.getMilliseconds();
Test See‹/›

getTime()方法

getTime()方法返回自1970 years1Month1milliseconds since the beginning of the day.

var d = new Date();
d.getTime();
Test See‹/›

一天(24小时)为8,640万毫秒。

修改日期(设置)

对于上面我们学到的所有get方法,都有一个对应的set方法。

如果使用getTo retrieve specific components from a date, usesetto modify the components of the date.

The following table is a list of Date objectsetThe table of methods:

MethodDate/TimeScope
setFullYear()Set the full yearYYYY
setMonth()Set the month0-11(0 = January)
setDate()Set to a specific day (of the current month)1-31
setDay()Set the day of the week0-6(0 = Sunday)
setHours()Set hours0-23
setMinutes()Set minutes0-59
setSeconds()Set seconds0-59
setMilliseconds()Set milliseconds0-999
setTime()Set the timestamp

By specifying from 1970-1-1 Set the time of the date object using the number of milliseconds elapsed since 00:00:00 UTC

For dates earlier than 1970-1-1 00:00:00 UTC can use negative values.

setFullYear() method

setFullYear()Method sets the year of the date object.

var d = new Date();
d.setFullYear(2010);
Test See‹/›

setFullYear()Method can choose to set the month and day.

var d = new Date();
d.setFullYear(2010, 8, 30);
Test See‹/›

setMonth() method

setMonth()Method sets the month of the date object (0-11).

var d = new Date();
d.setMonth(11);
Test See‹/›

setDate() method

setDate()Method sets the day of the month for the date object (01-31).

var d = new Date();
d.setDate(22);
Test See‹/›

setHours() method

setHours()Method sets the hours of the date object (0-23).

var d = new Date();
d.setHours(18);
Test See‹/›

setMinutes() method

setMinutes()Method sets the minutes of the date object (0-59).

var d = new Date();
d.setMinutes(32);
Test See‹/›

setSeconds() method

setSeconds()Method sets the seconds of the date object (0-59).

var d = new Date();
d.setSeconds(24);
Test See‹/›

setMilliseconds() method

setMilliseconds()Method sets the milliseconds of the date object (0-999).

var d = new Date();
d.setMilliseconds(420);
document.getElementById('result').innerHTML = d.getMilliseconds();
Test See‹/›

setTime() method

setTime()Method sets the Date object to the time since1970 years1Month1milliseconds since the beginning of the day.

var d = new Date();
d.setTime(1542655292087);
Test See‹/›

Use date methods with UTC

    The ones discussed abovegetMethod retrieves date components based on the user's local time zone settings.

To better specify dates and times, you can usegetUTCMethod, which is exactly the same as the get method, the difference is that they calculate time based on the UTC (Coordinated Universal Time) standard.

The following table is a list of UTC methods for the JavaScript Date object:

MethodDate/TimeScope
getUTCFullYear()YearYYYY
getUTCMonth()Month0-11(0 = January)
getUTCDate()Specific Day (of the Month)1-31
getUTCDay()Day of the Week0-6(0 = Sunday)
getUTCHours()Hours0-23
getUTCMinutes()Minutes0-59
getUTCSeconds()Seconds0-59
getUTCMilliseconds()Milliseconds0-999

To know the difference between the local time (local) and UTC get methods, we can run the following code:

let now = new Date();
// Print Local Time and UTC Time Zone (local and UTC)
document.write(now.getHours());
document.write(now.getUTCHours());
Test See‹/›

Running this code will print the current time and the UTC time zone time. If you are currently in the UTC time zone, the number output by the above program will be the same.

More Examples

In this example, JavaScript displays the current time.

10:38:2
Test See

Complete Date Reference

For a complete reference to properties and methods, please visit ourJavaScript Date Reference.

The reference section includes descriptions and examples of all Date properties and methods.