Js Date Date object

Date objects are used to process dates and times.


Return the current timeDate()

let time = Date(a);console.log(time);	//"Thu Mar 25 2021 14:57:38 GMT+0800"
Copy the code

2. Obtain the timestampgetTime()

let time = new Date().getTime();
console.log(time); / / 1616654985921
Copy the code

3, return year, month, day, hour, minute, second, week

let time1 = new Date().getFullYear();
let time2 = new Date().getMonth() + 1;
let time3 = new Date().getDate();
let time4 = new Date().getHours();
let time5 = new Date().getMinutes();
let time6 = new Date().getSeconds();
let time7 = new Date().getDay();

console.log(time1); / / 2021
console.log(time2); / / 3
console.log(time3); / / 25
console.log(time4); / / 15
console.log(time5); / / 6
console.log(time6); / / 55
console.log(time7); / / 4

Copy the code

4. Set timesetFullYear()

let time  new Date(a); time.setFullYear(2020.10.1)
console.log(time); //Sun Nov 01 2020 15:16:36 GMT+0800
Copy the code

5, transformation timetoUTCString(): ** Converts the date of the day (in UTC) to a string **

let time = Date(a);console.log(time);	//"Thu Mar 25 2021 15:23:00 GMT+0800"
console.log(time.toUTCString());	//"Thu, 25 Mar 2021 07:23:00 GMT"
Copy the code

6, the timestamp to format the date

// For example, the format yyyY-MM-dd hh: MM :ss is required
/* * @description Format timeStamp * @param {timeStamp to be processed} timeStamp:Number */
function formatTimestamp(timeStamp) {
    let date = new Date(timeStamp);
    Y = date.getFullYear() + The '-';
    M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + The '-';
    D = date.getDate() + ' ';
    h = date.getHours() + ':';
    m = date.getMinutes() + ':';
    s = date.getSeconds();
    let formatData = Y + M + D + h + m + s;
    return formatData
}
formatTimestamp(1626403785659);
// Output: 2021-07-16 10:49:45
Copy the code

One line of code implements the formatting timestamp

/* * @description Format timeStamp * @param {timeStamp to be processed} timeStamp:Number */
function formatTimestamp(timeStamp) {
    let date = new Date(time + 8 * 3600 * 1000);
    return date.toJSON().substr(0.19).replace('T'.' ').replace(/-/g.'. ');
}
formatTimestamp(1626403785659);  / / the 2021-07-16 10:49:45

//* The unary plus sign can strongly convert the current time into a timestamp
formatTimestamp(+new Date());  // Return the date formatted with the current timestamp

Copy the code