JS计算前一天或后一天,前一月后一月

JS计算前一天或后一天,前一月后一月,上一天下一下,上一月下一月。

方法一:


function ktkGetNextMonth(currentDate, scaleStep) {
    //scaleStep = 1 向后计算后一个月
    //scaleStep = -1 向前计算前一个月
        for(var minIncrements = 1;; minIncrements++) {
            var nextDate = new Date(currentDate);
            nextDate.setHours(currentDate.getHours() + scaleStep * minIncrements);

            if(nextDate.getMonth() != currentDate.getMonth()) {
                return nextDate;
            }
        }
    }
function ktkGetNextDate(currentDate, scaleStep) {
  //scaleStep = 1 向后计算后一天
  //scaleStep = -1 向前计算前一天
    for(var minIncrements = 1;; minIncrements++) {
        var nextDate = new Date(currentDate);
        nextDate.setHours(currentDate.getHours() + scaleStep * minIncrements);

        if(nextDate.getDate() != currentDate.getDate()) {
            return nextDate;
        }
    }
}

 

方法二:


function GetDateStr(AddDayCount) { 
    var dd = new Date(); 
        dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期 
        var y = dd.getFullYear(); 
        var m = dd.getMonth()+1;//获取当前月份的日期 
        var d = dd.getDate(); 
        return y+"-"+m+"-"+d; 
} 
document.write("前天:"+GetDateStr(-2)); 
document.write("<br />昨天:"+GetDateStr(-1)); 
document.write("<br />今天:"+GetDateStr(0)); 
document.write("<br />明天:"+GetDateStr(1)); 
document.write("<br />后天:"+GetDateStr(2)); 
document.write("<br />大后天:"+GetDateStr(3));
posted @ 2017-06-01 17:34  程序媛李李李李蕾  阅读(2365)  评论(0编辑  收藏  举报