获取今天,昨天,前天,明天,后天的日期以此类推

// 获取今天,昨天,前天,明天,后天的日期以此类推let yesterday = getDay(-1, '-');
getDay(num, str) {
    var today = new Date();
    var nowTime = today.getTime();
    var ms = 24*3600*1000*num;
    today.setTime(parseInt(nowTime + ms));
    var oYear = today.getFullYear();
    var oMoth = (today.getMonth() + 1).toString();
    if (oMoth.length <= 1) oMoth = '0' + oMoth;
    var oDay = today.getDate().toString();
    if (oDay.length <= 1) oDay = '0' + oDay;
    return oYear + str + oMoth + str + oDay;
}
let time = getDay(0, '-') //今天
let time = getDay(-1, '-') //昨天
let time = getDay(1, '-') //明天
以此类推的,后面的‘-’是时间的分隔符

 

posted @ 2022-09-14 14:39  Private!  阅读(72)  评论(0编辑  收藏  举报