Home  >  Article  >  Web Front-end  >  Detailed introduction to Date object in JavaScript (code example)

Detailed introduction to Date object in JavaScript (code example)

不言
不言forward
2019-01-07 10:05:353084browse

This article brings you a detailed introduction (code example) about the Date object in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Date instances are used to handle dates and times. The Date object is based on the number of milliseconds since January 1, 1970 (UTC).

JavaScript's Date object provides several UTC time methods, and also provides local time methods accordingly. UTC, which is what we call Greenwich Time, refers to the world time standard in time. The local time refers to the time set by the client computer executing JavaScript.

Date constructor

new Date();
//Sun Jan 06 2019 20:18:04 GMT+0800 (中国标准时间)

new Date(value); 
//value 代表自1970年1月1日00:00:00 (世界标准时间) 起经过的毫秒数。
new Date(000000000000);

//Thu Jan 01 1970 08:00:00 GMT+0800 (中国标准时间)

new Date(dateString);
//dateString表示日期的字符串值。该字符串应该能被 Date.parse() 方法识别
new Date("2019.01.01");
//Tue Jan 01 2019 00:00:00 GMT+0800 (中国标准时间)

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

//year代表年份的整数值。为了避免2000年问题最好指定4位数的年份; 使用 1998, 而不要用 98.
//month代表月份的整数值从0(1月)到11(12月)。
//day代表一个月中的第几天的整数值,从1开始。
//hour代表一天中的小时数的整数值 (24小时制)。
// minute分钟数。
// second秒数。
//millisecond表示时间的毫秒部分的整数值。

new Date(2019,01,01,01,01,01);

//Fri Feb 01 2019 01:01:01 GMT+0800 (中国标准时间)

Date method

Date.now()

Returns since 1970 -1-1 00:00:00 UTC (Universal Standard Time) The number of milliseconds that have elapsed so far, type is Number.

Date.now()
//1546777708417

Date.parse()

Parse a string representing the date and return the number of milliseconds elapsed from 1970-1-1 00:00:00 .

Date.parse("2019.01.01")
//1546272000000

Date.parse('01 Jan 1970 00:00:00 GMT');
//0

Date.UTC()

Accepts the same arguments as the longest form of the constructor (from 2 to 7), and returns values ​​from 1970-01- The number of milliseconds since 01 00:00:00 UTC.

  • year: A certain year after 1900.

  • month: An integer between 0 and 11, representing the month.

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

  • 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.

new Date(Date.UTC(2019, 0, 0, 0, 0, 1));
//Mon Dec 31 2018 08:00:01 GMT+0800 (中国标准时间)

Time stamp format conversion

  dateFormmat(time) {
    let date = new Date(time * 1000); //如果date为13位不需要乘1000
    let Ye = date.getFullYear() + '/';
    let Mo =
      (date.getMonth() + 1 < 10
        ? '0' + (date.getMonth() + 1)
        : date.getMonth() + 1) + '/';
    let Da =
      (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
    let hh =
      (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    let mm =
      (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
      ':';
    let ss =
      date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    return Ye + Mo + Da +hh + mm + ss
  }
  
//let value=dateFormmat(1234567890)
//console.log(value)
//2009/02/14 07:31:30

Date instance-(get)

All Date instances inherit from Date .prototype. Modifying the prototype object of the Date constructor affects all Date instances.

Date.getDate()

Returns a specified date object as the day of the month based on local time. getDate() returns an integer value from 1 to 31

let date = new Date("December 25, 2019 11:11:00");
let day = date.getDate();

console.log(day)
//25

Date.getDay()

getDay() returns an integer value: 0 represents Sunday, 1 represents Monday, 2 represents Tuesday, By analogy

Date.getFullYear()

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

Date.getMonth()

Returns the month of a specified date object based on local time, as a 0-based value (0 represents the first month of the year).

Date.getHours()

The getHours() method returns the hour of a specified date object based on local time. getHours returns an integer value between 0 and 23.

Date.getMinutes()

The getMinutes() method returns the minutes of a specified date object based on local time. getMinutes returns an integer value from 0 to 59

Date.getSeconds()

getSeconds() method returns the seconds of a specified date object based on local time, and returns an integer value from 0 to 59 Integer value.

Date.getMilliseconds()

getMilliseconds() method returns the number of milliseconds in a specified date object based on local time. The getMilliseconds() method returns an integer from 0 to 999.

Date.getTime()

The return value of the getTime method is a numerical value, indicating the distance from the date at 0:00:00 on January 1, 1970 (UTC, that is, Coordinated Universal Time) The number of milliseconds in time represented by the object.

Date instance-(set)

Date.setDate()

setDate() method specifies the number of days in a date object based on local time.
If dayValue is outside the reasonable range of the month, setDate will update the Date object accordingly. For example, if you specify 0 for dayValue, the date is set to the last day of the previous month.

Date.setFullYear()

The setFullYear() method sets the year for a date object based on local time
If one parameter exceeds a reasonable range, the setFullYear method will update other parameter values , the date value of the date object will also be updated accordingly. For example, specifying 15 for monthValue will increase the year by 1 and the month value will be 3.

Date.setHours()

The setHours() method sets the hours for a date object based on local time and returns the updated date from 1970-01-01 00:00:00 UTC The number of milliseconds in time represented by the object instance.

If a parameter exceeds the reasonable range, setHours will update the date information in the date object accordingly. For example, if 100 is specified for secondsValue, the minutes are incremented by 1, and then 40 is used for seconds.

Date.setMilliseconds()

The setMilliseconds() method sets the milliseconds of a date object based on local time.

If the specified number is outside a reasonable range, the time information of the date object will be updated accordingly. For example, if 1005 is specified, the number of seconds is increased by 1 and the number of milliseconds is 5.

Date.setMinutes()

The setMinutes() method sets the minutes for a date object based on local time.

If a specified parameter exceeds a reasonable range, setMinutes will update the time information in the date object accordingly. For example, specifying 100 for secondsValue will increase the number of minutes by 1 and the number of seconds will be 40.

Date.setMonth()

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

If a specified parameter exceeds a reasonable range, setMonth will respond accordingly Update the date information in the date object. For example, specifying 15 for monthValue increases the year by 1 and uses 3 for the month.

Date.setSeconds()

The setSeconds() method sets the seconds of a date object based on local time.

If a parameter exceeds a reasonable range, the setSeconds method will update the time information of the date object accordingly. For example, specifying 100 for secondsValue will increase the date object's minutes by 1 and use 40 for seconds.

Date.setTime()

The setTime() method sets the time for a Date object with a number of milliseconds representing the time since 1970-1-1 00:00:00 UTC.

https://developer.mozilla.org...

The above is the detailed content of Detailed introduction to Date object in JavaScript (code example). 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