JavaScript Date object time processing

paraphrase

Date objects are used to work with times and dates;
create a JavaScript Date instance that represents a moment in time. Date objects are based on the Unix Time Stamp, which is the number of milliseconds since January 1, 1970 (UTC).

grammar

new Date();
new Date(value);
new Date(dateString);
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);

Note: The only way to create a new Date object is through the new operator, for example: let now = new Date();
if it is called as a regular function (that is, without the new operator), it will return a string instead of Date object.

Date can be constructed in four ways (as described in the syntax)

  1. No arguments new Date(): The created Date object represents the date and time at the moment of instantiation.
  2. Unix Timestamp new Date(value): A Unix Time Stamp (Unix Time Stamp), which is an integer value representing the number of milliseconds since January 1, 1970 00:00:00 UTC (the Unix epoch), ignoring leap seconds. Note that most Unix timestamp functions are only accurate to the nearest second.
  3. Timestamp String new Date(dateString): A string value representing a date. The string should be recognized by the correct method of Date.parse() (ie conforming to IETF-compliant RFC 2822 timestamps or version of ISO8601).
  4. Date and hour, minute and second new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]):
    year represents the integer value of the year.
    0 to 99 are mapped to the years 1900 to 1999, other values ​​represent actual years.
    monthIndex
    represents the integer value of the month, from 0 (January) to 11 (December).
    date Optional
    An integer value representing the day of the month, starting from 1. The default value is 1.
    hours Optional
    An integer value representing the hours of the day (24-hour clock). The default is 0 (midnight).
    minutes Optional
    Integer value representing the minutes part of a complete time (such as 01:10:00). The default value is 0.
    seconds Optional
    Integer value representing the seconds part of a complete time (such as 01:10:00). The default value is 0.
    milliseconds optional
    An integer value representing the millisecond portion of a full time. The default value is 0.

Pay attention to the difference when using timestamp string to get Date instance

Due to differences and inconsistencies between browsers, using the Date constructor for parsing date strings (or using its equivalent Date.parse ) is strongly deprecated. There is only convention support for dates in RFC 2822 format. In ISO 8601 format support, only date strings (such as "1970-01-01") will be processed as UTC instead of local time, which is different from strings in other formats.

Related standard links RFC2822 Section 3.3 && ISO 8601

Get and set methods

Note: Non-UTC (Universal Standard Time)

method describe return value
getDate() Returns the day of the month (1 ~ 31) from a Date object. integer
getDay() Returns the day of the week (0 ~ 6) from a Date object. integer
getFullYear() Returns the year from a Date object as a four-digit number. integer
getHours() Returns the hour (0 ~ 23) of the Date object. integer
getMilliseconds() Returns the milliseconds (0 ~ 999) of the Date object. integer
getMinutes() Returns the minute (0 ~ 59) of the Date object. integer
getMonth() Returns the month (0 ~ 11) from a Date object. integer
getSeconds() Returns the seconds (0 ~ 59) of the Date object. integer
getTime() Returns the number of milliseconds since January 1, 1970 00:00:00 to the present. integer
toDateString() Converts the date portion of a Date object to a string. string
toISOString() Returns the date format of the string using the ISO standard. string
toJSON() Returns a date string in JSON data format. string
toLocaleDateString() Converts the date portion of a Date object to a string according to the local time format. string
toLocaleTimeString() Converts the time portion of a Date object to a string according to the local time format. string
toLocaleString() Converts a Date object to a string according to the local time format. string
toString() Convert a Date object to a string. string
toTimeString() Converts the time portion of a Date object to a string. string
parse() Returns the number of milliseconds from midnight, January 1, 1970 to the specified date (string). integer or NaN
setDate() Set the day of the month (1 ~ 31) in the Date object.
setFullYear() Sets the year (four digits) in the Date object.
setHours() Sets the hour (0 ~ 23) in the Date object.
setMilliseconds() Sets the milliseconds (0 ~ 999) in the Date object.
setMinutes() Sets the minute (0 ~ 59) in the Date object.
setMonth() Set the month (0 ~ 11) in the Date object.
setSeconds() Sets the second (0 ~ 59) in the Date object.
setTime() method sets the Date object in milliseconds.

Get Time

/*以当前时间为例子*/
var nowTime = new Date(); 
//Wed Jul 13 2022 22:27:34 GMT+0800 (中国标准时间)
console.log(nowTime.getFullYear());
//2022
console.log(nowTime.getMonth());
//6
console.log(nowTime.getDate());
//13
console.log(nowTime.getHours());
//22
console.log(nowTime.getMinutes());
//27
console.log(nowTime.getSeconds());
//34
console.log(nowTime.getDay());
//3
console.log(nowTime.getTime());
//1657722454269
/*提取到需要的时间数据进行拼接获取需要的数据*/

set time

/*修改以获取的当前时间对象,为准确性用已经生成的时间获取新时间*/
var nowTime = new Date('Wed Jul 13 2022 22:27:34 GMT+0800');
console.log(nowTime.setFullYear(2020)+', nowTime:'+nowTime);
//1594650454269, nowTime:Mon Jul 13 2020 22:27:34 GMT+0800 (中国标准时间)
console.log(nowTime.setMonth(5)+', nowTime:'+nowTime);
//1592058454269, nowTime:Sat Jun 13 2020 22:27:34 GMT+0800 (中国标准时间)
console.log(nowTime.setDate(20)+', nowTime:'+nowTime);
//1592663254269, nowTime:Sat Jun 20 2020 22:27:34 GMT+0800 (中国标准时间)
console.log(nowTime.setHours(20)+', nowTime:'+nowTime);
//1592656054269, nowTime:Sat Jun 20 2020 20:27:34 GMT+0800 (中国标准时间)
console.log(nowTime.setMinutes(20)+', nowTime:'+nowTime);
//1592655634269, nowTime:Sat Jun 20 2020 20:20:34 GMT+0800 (中国标准时间)
console.log(nowTime.setSeconds(20)+', nowTime:'+nowTime);
//1592655620269, nowTime:Sat Jun 20 2020 20:20:20 GMT+0800 (中国标准时间)

References:
1. "Date - JavaScript | MDN";
2. "JavaScript Date Object | Novice Tutorial";

Guess you like

Origin blog.csdn.net/Mr_Bobcp/article/details/125773635