Home  >  Article  >  Web Front-end  >  Detailed explanation of Date object in JavaScript (with examples)

Detailed explanation of Date object in JavaScript (with examples)

不言
不言forward
2018-10-18 16:34:312285browse

This article brings you a detailed explanation of the Date object in JavaScript (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Create a Date instance object

1.new Date(); Create a Date object based on the current time set by the system

    let today = new Date();
    console.log(today); //Thu Jun 14 2018 14:51:00 GMT+0800 (CST)

2.new Date(value);

Parameter: value represents since January 1, 1970 00:00:00 (Universal Standard Time) The number of milliseconds passed

    let day = new Date(1528959690000);
    console.log(day); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST)

3.new Date(dateString);

Parameter: dateString A string value representing the date. This string should be recognized by the Date.parse() method

    let day1 = new Date("2018-06-14T07:01:30.000Z");
    console.log(day1); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST)

4.new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]] ]);

Parameters:

  • year represents the integer value of the year. In order to avoid the 2000 problem, it is best to specify a 4-digit year; use 1998, Instead of using 98

  • month represents the integer value of the month from 0 (January) to 11 (December)

  • day represents a month The integer value of the day in , starting from 1

  • hour The integer value representing the number of hours in a day (24-hour format)

  • minute Number of minutes

  • second Number of seconds

  • millisecond An integer value representing the millisecond part of time

    let day2 = new Date(2018,5,14);
    console.log(day2); //Thu Jun 14 2018 00:00:00 GMT+0800 (CST)

    let day3 = new Date(2018,5,14,15,1,30);
    console.log(day3); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST)

2. Date static properties

##1.Date.prototype represents the prototype of the Date constructor

Date Instances inherit from Date.prototype. You can use the prototype object of the constructor to add properties or methods to all Date instances

3. Date static methods

1.Date.UTC() method returns the number of milliseconds from 1970-1-1 00:00:00 UTC to the specified date

Syntax: Date.UTC(year, month[,date[,hrs[,min[,sec[,ms]]]]])

Parameters:

  • year A certain year after 1900

  • month An integer between 0 and 11, indicating the month

  • date An integer between 1 and 31, indicating the day of the month Day

  • hrs An integer between 0 and 23, representing hours

  • min An integer between 0 and 59, representing minutes

  • sec An integer between 0 and 59, representing seconds

  • ms An integer between 0 and 999, representing milliseconds

    let utcDate = new Date(Date.UTC(2018, 5, 14, 7, 1, 30));
    console.log(Date.UTC(2018, 5, 14, 7, 1, 30)); //1528959690000
    console.log(utcDate); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST)

2.Date.now() method returns the number of milliseconds from 00:00:00 UTC on January 1, 1970 to the current time

    let nowDate = new Date(Date.now());
    console.log(nowDate); //Fri Jun 15 2018 16:31:05 GMT+0800 (CST)
    console.log(Date.now()); //1529051465358

3.Date.parse() method returns a value representing the number of milliseconds from 1970-1-1 00:00:00 UTC to the time represented by the given date string

Syntax: Date.parse(dateString)

Parameters: dateString A string that conforms to RFC2822 or ISO 8601 date format

Return value: Returns a string from 1970-1-1 00:00:00 The number of milliseconds from UTC to the time represented by the given date string. If the parameter cannot be parsed into a valid date, NaN

    let time = Date.parse('2018-06-14T07:01:30.000Z');
    console.log(time);// 1528959690000

is returned. 4. Date instance method

1.Year

setFullYear() method sets the year for a date object based on local time

Syntax: dateObj.setFullYear(yearValue[, monthValue[, dayValue]])

Parameters:

  • yearValue Specifies the integer value of the year, such as 1995.

  • monthValue An integer value between 0 and 11, representing January to December.

  • dayValue An integer value between 1 and 31, indicating the day of the month. If you specify the dayValue parameter, you must also specify the monthValue

The getFullYear() method returns the year of the specified date based on local time

Syntax: dateObj.getFullYear()

Return value: Returns a year number corresponding to the given date based on local time

    let date1 = new Date(1528959690000);
    console.log(date1); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST)
    console.log(date1.setFullYear(2017)); //1497423690000
    console.log(date1.getFullYear()); //2017

setUTCFullYear() method sets the year for a specific date based on universal standard time

Syntax: dateObj.setUTCFulYear(yearValue[, monthValue[, dayValue]])

Parameters:

  • yearValue Specifies the year integer value, for example, 1995

  • monthValue Optional, specify an integer value between 0-11, representing January to December

  • dayValue Optional , specify an integer value between 1-31, representing the day of the month. If you specify the dayValue parameter, then you must specify the monthValue parameter

getUTCFulYear( ) Based on universal time, returns the year of a specified date object

Syntax: dateObj.getUTCFullYear()

Return value: Returns an absolute value, conforming to the Year-2000 standard, such as 1995

let day1 = new Date(1528959690000);
console.log(day1.toUTCString()); //Thu, 14 Jun 2018 07:01:30 GMT
console.log(day1.setUTCFullYear(2017)); //1497423690000
console.log(day1.getUTCFullYear()); //2017

2. Month

setMonth() method sets the month for a date object with a set year based on local time

Syntax: dateObj.setMonth(monthValue[, dayValue])

Parameters:

  • monthValue  介于 0 到 11 之间的整数(表示一月到十二月)

  • dayValue  从 1 到 31 之间的整数,表示月份中的第几天

getMonth() 方法根据本地时间,返回一个指定的日期对象的月份

语法:dateObj.getMonth()
返回值:返回一个0 到 11的整数值: 0 代表一月份,1 代表二月份, 2 代表三月份,依次类推

    let date2 = new Date(1528959690000);
    console.log(date2); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST)
    console.log(date2.setMonth(4)); //1526281290000
    console.log(date2.getMonth()); //4

setUTCMonth()方法根据通用的时间来设置一个准确的月份

语法:dateObj.setUTCMonth(monthValue[, dayValue])
参数:

  • monthValue  一个0-11的整数,代表1月到12月

  • dayValue  可选参数,一个1-31的整数,代表一个月的天数

getUTCMonth方法以世界时为标准,返回一个指定的日期对象的月份,它是从 0 开始计数的(0 代表一年的第一个月)

语法:dateObj.getUTCMonth()
返回值:返回一个 0 到 11 的整数,分别对应以下月份:0 代表一月,1 代表二月,2 代表三月,依次类推

    let day2 = new Date(1528959690000);
    console.log(day2.toUTCString()); //Thu, 14 Jun 2018 07:01:30 GMT
    console.log(day2.setUTCMonth(4)); //1526281290000
    console.log(day2.getUTCMonth()); //4

3.日

setDate() 方法根据本地时间来指定一个日期对象的天数

语法:dateObj.setDate(dayValue)
参数:dayValue  一个整数,表示该月的第几天

getDate() 根据本地时间,返回一个指定的日期对象为一个月中的第几天

语法:dateObj.getDate()
返回值:返回一个1 到 31的整数值

    let date3 = new Date(1528959690000);
    console.log(date3); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST)
    console.log(date3.setDate(21)); //1529564490000
    console.log(date3.getDate()); //21

setUTCDate() 方法就是根据全球时间设置特定date对象的日期

语法:dateObj.setUTCDate(dayValue)
参数:dayValue  一个1-31的整形数字,用来指定日期

getUTCDate() 方法以世界时为标准,返回一个指定的日期对象为一个月中的第几天

语法:dateObj.getUTCDate()
返回值:返回一个 1 到 31 的整数值

    let day3 = new Date(1528959690000);
    console.log(day3.toUTCString()); ////Thu, 14 Jun 2018 07:01:30 GMT
    console.log(day3.setUTCDate(25)); //1529910090000
    console.log(day3.getUTCDate()); //25

4.星期几

getDay() 方法根据本地时间,返回一个具体日期中一周的第几天,0 表示星期天

语法:dateObj.getDay()
返回值:返回一个整数值: 0 代表星期日, 1 代表星期一,2 代表星期二, 依次类推

    let date = new Date(1528959690000);
    console.log(date); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST)
    console.log(date.getDay()); //4

getUTCDay() 方法以世界时为标准,返回一个指定的日期对象为一星期中的第几天,其中 0 代表星期天

语法:dateObj.getUTCDay()
返回值:返回一个对应一星期中第几天的整数:0 代表星期天,1 代表星期一,2 代表星期二,依次类推

5.时

setHours() 方法根据本地时间为一个日期对象设置小时数

语法:dateObj.setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
参数:

  • hoursValue  一个 0 到 23 的整数,表示小时

  • minutesValue  一个 0 到 59 的整数,表示分钟

  • secondsValue  一个 0 到 59 的整数,表示秒数,如果指定了 secondsValue 参数,则必须同时指定 minutesValue 参数

  • msValue  一个 0 到 999 的数字,表示微秒数,如果指定了 msValue 参数,则必须同时指定 minutesValue 和 secondsValue 参数

getHours() 方法根据本地时间,返回一个指定的日期对象的小时

语法:dateObj.getHours()
返回值:返回一个0 到 23之间的整数值

    let date4 = new Date(1528959690000);
    console.log(date4); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST)
    console.log(date4.setHours(11)); //1528945290000
    console.log(date4.getHours()); //11

setUTCHours() 方法就是根据全球时间设置特定date对象的小时数

语法:dateObj.setUTCHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
参数:

  • hoursValue  表示小时的整数,取值0到23之间

  • minutesValue  可选参数,表示分钟的整数,取值0到59之间

  • secondsValue  可选参数,表示秒数的整数,取值0到59之间;如果指定了该参数,就要同时指定minutesValue这个参数

  • msValue  可选参数,表示毫秒的整数,取值0到999之间;如果指定了该参数,就要指定minutesValue和secondsValue这两个参数

getUTCHours() 方法以世界时为标准,返回一个指定的日期对象的小时数

语法:dateObj.getUTCHours()
返回值:返回一个 0 到 23 的整数

    let day4 = new Date(1528959690000);
    console.log(day4.toUTCString()); //Thu, 14 Jun 2018 07:01:30 GMT
    console.log(day4.setUTCHours(15)); //1528988490000
    console.log(day4.getUTCHours()); //15

6.分

setMinutes() 方法根据本地时间为一个日期对象设置分钟数

语法:dateObj.setMinutes(minutesValue[, secondsValue[, msValue]])
参数:

  • minutesValue  一个 0 到 59 的整数,表示分钟数。

  • secondsValue  一个 0 到 59 的整数,表示秒数,如果指定了 secondsValue 参数,则必须同时指定 minutesValue 参数。

  • msValue  一个 0 到 999 的数字,表示微秒数,如果指定了 msValue 参数,则必须同时指定 minutesValue和secondsValue 参数

getMinutes() 方法根据本地时间,返回一个指定的日期对象的分钟数

语法:dateObj.getMinutes()
返回值:返回一个0 到 59的整数值

    let date5 = new Date(1528959690000);
    console.log(date5); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST)
    console.log(date5.setMinutes(30)); //1528961430000
    console.log(date5.getMinutes()); //30

setUTCMinutes()方法会根据根据全球时间来设置指定日期的分钟数

语法:dateObj.setUTCMinutes(minutesValue[, secondsValue[, msValue]])
参数:

  • minutesValue  必填,表示要设置的分钟数,是一个介于0和59之间的整数

  • secondsValue  可选参数,表示要设置的秒数,同样也是一个介于0和59之间的整数,如果你传入了这个参数,那么你就必须要传入minutesValue

  • msValue  可选参数,表示要设置的毫秒数,这是一个介于0和999之间的数字,如果你传入了这个参数,那么你就必须要传入minutesValue和secondsValue

getUTCMinutes() 方法以世界时为标准,返回一个指定的日期对象的分钟数

语法:dateObj.getUTCMinutes()
返回值:返回一个 0 到 59 的整数

    let day5 = new Date(1528959690000);
    console.log(day5.toUTCString()); //Thu, 14 Jun 2018 07:01:30 GMT
    console.log(day5.setUTCMinutes(45)); //1528962330000
    console.log(day5.getUTCMinutes()); //45

7.秒

setSeconds() 方法根据本地时间设置一个日期对象的秒数

语法:dateObj.setSeconds(secondsValue[, msValue])
参数:

  • secondsValue  一个 0 到 59 的整数

  • msValue  一个 0 到 999 的数字,表示微秒数

getSeconds() 方法根据本地时间,返回一个指定的日期对象的秒数

语法:dateObj.getSeconds()
返回值:返回一个 0 到 59 的整数值

    let date6 = new Date(1528959690000);
    console.log(date6); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST)
    console.log(date6.setSeconds(40)); //1528959700000
    console.log(date6.getSeconds()); //40

setUTCSeconds() 方法为一个依据国际通用时间的特定日期设置秒数

语法:dateObj.setUTCSeconds(secondsValue[, msValue])
参数:

  • secondsValue  一个在0到59之间的整数,表示秒数

  • msValue  可选参数,一个0到999之间的数字,代表毫秒数

getUTCSeconds() 方法以世界时为标准,返回一个指定的日期对象的秒数

语法:dateObj.getUTCSeconds()
返回值:返回一个 0 到 59 的整数

    let day6 = new Date(1528959690000);
    console.log(day6.toUTCString()); //Thu, 14 Jun 2018 07:01:30 GMT
    console.log(day6.setUTCSeconds(50)); //1528959710000
    console.log(day6.getUTCSeconds()); //50

8.毫秒

setMilliseconds() 方法会根据本地时间设置一个日期对象的豪秒数

语法:dateObj.setMilliseconds(millisecondsValue)
参数:millisecondsValue  一个 0 到 999 的数字,表示豪秒数

getMilliseconds() 方法,根据本地时间,返回一个指定的日期对象的毫秒数

语法:dateObj.getMilliseconds()
返回值:方法返回一个0 到 999的整数

let date7 = new Date(1528959690000);
console.log(date7); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST)
console.log(date7.setMilliseconds(100)); //1528959690100
console.log(date7.getMilliseconds()); //100

setUTCMilliseconds() 方法会根据世界时来设置指定时间的毫秒数

语法:dateObj.setUTCMilliseconds(millisecondsValue)
参数:millisecondsValue  0 - 999 之间的数值,代表毫秒数

getUTCMilliseconds() 方法以世界时为标准,返回一个指定的日期对象的毫秒数

语法:dateObj.getUTCMilliseconds()
返回值:返回一个 0 到 999 的整数

    let day7 = new Date(1528959690000);
    console.log(day7.toUTCString()); //Thu, 14 Jun 2018 07:01:30 GMT
    console.log(day7.setUTCMilliseconds(900)); //1528959690900
    console.log(day7.getUTCMilliseconds()); //900

9.time

setTime() 方法以一个表示从1970-1-1 00:00:00 UTC计时的毫秒数为来为 Date 对象设置时间

语法:dateObj.setTime(timeValue)
参数:timeValue  一个整数,表示从1970-1-1 00:00:00 UTC开始计时的毫秒数

getTime() 方法返回一个时间的格林威治时间数值,这个方法的功能和 valueOf() 方法一样

语法:dateObj.getTime()
返回值:返回一个从1970年1月1日0时0分0秒(UTC,即协调世界时)距离该日期对象所代表时间的毫秒数

    let date8 = new Date(1528959690000);
    console.log(date8); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST)
    console.log(date8.setTime(1528959690100)); //1528959690100
    console.log(date8.getTime()); //1528959690100

10.获取时间字符串

  • toDateString() 方法以美式英语和人类易读的形式返回一个日期对象日期部分的字符串

  • toTimeString() 方法以人类易读形式返回一个日期对象时间部分的字符串

  • toUTCString() 方法把一个日期转换为一个字符串,使用UTC时区

  • toISOString() 方法返回一个 ISO(ISO 8601 Extended Format)格式的字符串:
     YYYY-MM-DDTHH:mm:ss.sssZ

  • toJSON() 方法返回 Date 对象的字符串形式

  • toLocaleString() 方法返回该日期对象的字符串,该字符串格式因不同语言而不同

  • toLocaleDateString() 方法返回该日期对象日期部分的字符串,该字符串格式因不同语言而不同

  • toLocaleTimeString() 方法返回该日期对象时间部分的字符串,该字符串格式因不同语言而不同

  • valueOf() 方法返回一个 Date 对象的原始值

let day9 = new Date(1528959690000);
console.log(day9); //Thu Jun 14 2018 15:01:30 GMT+0800 (中国标准时间)
console.log(day9.toString()); //Thu Jun 14 2018 15:01:30 GMT+0800 (中国标准时间)
console.log(day9.toDateString()); //Thu Jun 14 2018
console.log(day9.toTimeString()); //15:01:30 GMT+0800 (中国标准时间)
console.log(day9.toUTCString()); //Thu, 14 Jun 2018 07:01:30 GMT
console.log(day9.toISOString()); //2018-06-14T07:01:30.000Z
console.log(day9.toJSON()); //2018-06-14T07:01:30.000Z
console.log(day9.toLocaleString()); //2018/6/14 下午3:01:30
console.log(day9.toLocaleDateString()); //2018/6/14
console.log(day9.toLocaleTimeString()); //下午3:01:30
console.log(day9.valueOf()); //1528959690000

The above is the detailed content of Detailed explanation of Date object in JavaScript (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete