JS獲取近3個月、近1個月、近1周的日期范圍


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<div id="last3Month"></div>
<br>
<div id="lastMonth"></div>
<br>
<div id="lastWeek"></div>
<script>
// 近3個月
function getLast3Month() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;//0-11表示1-12月
    var day = now.getDate();
    var dateObj = {};
    dateObj.now = year + '-' + month + '-' + day;
      var nowMonthDay = new Date(year, month, 0).getDate();    //當前月的總天數
    if(month - 3 <= 0){ //如果是1、2、3月,年數往前推一年
        var last3MonthDay = new Date((year - 1), (12 - (3 - parseInt(month))), 0).getDate();    //3個月前所在月的總天數
        if(last3MonthDay < day){    //3個月前所在月的總天數小於現在的天日期
            dateObj.last = (year - 1) + '-' + (12 - (3 - month)) + '-' + last3MonthDay;
        }else{
            dateObj.last = (year - 1) + '-' + (12 - (3 - month)) + '-' + day;
        }
    }else{
        var last3MonthDay = new Date(year, (parseInt(month) - 3), 0).getDate();    //3個月前所在月的總天數
        if(last3MonthDay < day){    //3個月前所在月的總天數小於現在的天日期
            if(day < nowMonthDay){        //當前天日期小於當前月總天數,2月份比較特殊的月份
                dateObj.last = year + '-' + (month - 3) + '-' + (last3MonthDay - (nowMonthDay - day));
            }else{
                dateObj.last = year + '-' + (month - 3) + '-' + last3MonthDay;
            }
        }else{
            dateObj.last = year + '-' + (month - 3) + '-' + day;
        }
    }
    return dateObj;
}
// 近一個月
function getLastMonth() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;//0-11表示1-12月
    var day = now.getDate();
    var dateObj = {};
    dateObj.now = year + '-' + month + '-' + day; 
    var nowMonthDay = new Date(year, month, 0).getDate();    //當前月的總天數
    if(month - 1 <= 0){ //如果是1月,年數往前推一年<br>     
        dateObj.last = (year - 1) + '-' + 12 + '-' + day;
    }else{
        var lastMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate();  
        if(lastMonthDay < day){    //1個月前所在月的總天數小於現在的天日期
            if(day < nowMonthDay){        //當前天日期小於當前月總天數
                dateObj.last = year + '-' + (month - 1) + '-' + (lastMonthDay - (nowMonthDay - day));
            }else{
                dateObj.last = year + '-' + (month - 1) + '-' + lastMonthDay;
            }
        }else{
            dateObj.last = year + '-' + (month - 1) + '-' + day;
        }
    }
    return dateObj;
}
// 近一周
function getLastWeek() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;//0-11表示1-12月
    var day = now.getDate();
    var dateObj = {};
    dateObj.now = year + '-' + month + '-' + day;
    if(day - 7 <= 0){   //如果在當月7日之前
        var lastMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate();    //1周前所在月的總天數
        if(month - 1 <= 0){ //如果在當年的1月份
            dateObj.last = (year - 1) + '-' + 12 + '-' + (31 - (7 - day));
        }else{
            dateObj.last = year + '-' + (month - 1) + '-' + (lastMonthDay - (7 - day));
        }
    }else{
        dateObj.last = year + '-' + month + '-' + (day -7);
    }
    return dateObj;
}
var dateObj1 = getLast3Month();
var dateObj2 = getLastMonth();
var dateObj3 = getLastWeek();
window.onload = function(){
    var last3Month = document.getElementById("last3Month").innerHTML = "近三個月:<br>" + dateObj1.now + '<br>' + dateObj1.last;
    var lastMonth = document.getElementById("lastMonth").innerHTML = "近一個月:<br>" + dateObj2.now + '<br>' + dateObj2.last;
    var lastWeek = document.getElementById("lastWeek").innerHTML = "近一周:<br>" + dateObj3.now + '<br>' + dateObj3.last;
}
 
</script><br>
</body>
</html>

 

 

參考文章:https://www.cnblogs.com/AIonTheRoad/p/11134097.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM