Use of Date in Js

1. Get today's 0: 0: 0 (usually used to get the start date)

var startDate = new Date (new Date (). toLocaleDateString ()); // Tue May 15 2018 00:00:00 GMT + 0800 (China Standard Time)
 

2. Get the date one month ago

var lastM = new Date (new Date (). setMonth (new Date (). getMonth ()-1)); // Sun Apr 15 2018 09:18:08 GMT + 0800 (China Standard Time)
 

3. Get 0: 0: 0 a month ago

var lastM_start = new Date (new Date (new Date (). toLocaleDateString ()). setMonth (new Date (). getMonth ()-1));
// Sun Apr 15 2018 00:00:00 GMT + 0800 (China standard Time)
 

4. Get the date of the previous day

var yesterday = new Date (new Date (). setDate (new Date (). getDate ()-1)); // Mon May 14 2018 09:26:39 GMT + 0800 (China Standard Time)
 

5. Get today's 23:59:59

var endDate = new Date (new Date (new Date (). toLocaleDateString ()). getTime () + 24 * 60 * 60 * 1000-1);
// Tue May 15 2018 23:59:59 GMT + 0800 (China standard Time) 
 

6. Get yesterday's 23:59:59

var yes_endDate = new Date(new Date(new Date(
new Date().setDate(new Date().getDate()-1)).toLocaleDateString()).getTime()+24*60*60*1000-1);
//Mon May 14 2018 23:59:59 GMT+0800 (中国标准时间)

 

https://www.w3school.com.cn/jsref/jsref_obj_date.asp

 

 

Example:

Get the date after the input time and return the date string

let endTime = '2020-01-01';
let endTime = formatDate(new Date(new Date(endTime).setDate(new Date(endTime).getDate()+1)));
console.log(endTime);//2020-01-02


//返回格式化日期 yyyy-MM-dd
function formatDate(date){
    return PrefixInteger(date.getFullYear(),4)+"-"+PrefixInteger(parseInt(date.getMonth()+1),2) +"-"+PrefixInteger(date.getDate(),2);
}
//数字转字符串,前方自动补零
function PrefixInteger(num, n) {
    return (Array(n).join(0) + num).slice(-n);
}

 

Published 83 original articles · Like 38 · Visit 230,000+

Guess you like

Origin blog.csdn.net/mao_mao37/article/details/105144629