格式化获取当前日期与设置日期通用方法

function changeDate (year_add,month_add,day_add) {
    let nowTime = new Date();
    nowTime.setFullYear(nowTime.getFullYear() + year_add);
    nowTime.setMonth(nowTime.getMonth() + month_add);
    nowTime.setDate(nowTime.getDate() + day_add);
    let nowYear = nowTime.getFullYear();
    let nowMonth = nowTime.getMonth()+1;
    let nowDay = nowTime.getDate();
    if(nowMonth < 10){
        nowMonth = "0" + nowMonth;
    }
    if(nowDay < 10){
        nowDay = "0" + nowDay;
    }
    return nowYear + '-' + nowMonth + '-' + nowDay;
}
console.log(changeDate(0,0,0)); //2020-02-21
console.log(changeDate(1,1,1)); //2021-03-22

评论

暂无评论