Method 1: Get the standard date format of the past time to determine the year, month, and day
Note:, new Date(yy,mm-4,dd);
is to get the standard date format of the past time. The mm obtained here is the month plus 1, so here is mm-4 instead of -3.
function getThreeMonthDate(){
let currDate=new Date().toLocaleDateString();// 2020/11/6
let arrs=currDate.split('/');
let yy=parseInt(arrs[0]);
let mm=parseInt(arrs[1]);
let dd=parseInt(arrs[2]);
let pdStr=new Date(yy,mm-4,dd).toLocaleDateString().split('/').join('-');
return pdStr; // 2020-8-6;可针对需要的格式进行优化。
}
Note: If you need to test, you can pass the test string into new Date() when defining currDate. For example, I want to test how long is today three months ago of '2019-5-29', let currDate=new Date('2019-5-29').toLocaleDateString(); The rest is unchanged, the result of calling the getThreeMonthDate() function is: 2019-3-1