js判断根据年月日获取七天前日期和七天后日期并判断日期是否大于7天

 

js判断根据年月日获取七天前日期和七天后日期并判断日期是否大于7天

 

     var date = '2021-03-02'
        var date1 = '2021-03-09'
        console.log(get7q(date))
        console.log(get7h(date))
        console.log(is7t(date,date1))
        
        //获取7天前日期
        function get7q(date){
            var date1 = new Date(date);
            var date2 = new Date(date1);
            date2.setDate(date1.getDate() - 7);
            var time = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
            return time
        }
        //获取7天后日期
        function get7h(date){
            
            var date1 = new Date(date);
            var date2 = new Date(date1);
            date2.setDate(date1.getDate() + 7);
            var time = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
            return time
        }
        //判断日期间隔是否大于7天
        function is7t(date,date1){
            var time1 = new Date(date);
            var time2 = new Date(date1);
            var day = time2.getDate() - time1.getDate() + 1
            return day > 7
        }

 

posted @ 2021-03-07 19:07  国际路线  阅读(1077)  评论(0编辑  收藏  举报