日期延迟

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
    </head>
    <body>
    </body>
    <script type="text/javascript">
        function GetDateStr(AddDayCount) {   
           var dd = new Date();  //获得时间戳
           dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期  
           var y = dd.getFullYear();   
           var m = (dd.getMonth()+1)<10?"0"+(dd.getMonth()+1):(dd.getMonth()+1);//获取当前月份的日期,不足10补0  
           var d = dd.getDate()<10?"0"+dd.getDate():dd.getDate();//获取当前几号,不足10补0  
           return y+"-"+m+"-"+d;   
        }  

        console.log('半年前' + GetDateStr(-180));
        
    </script>

    <script type="text/javascript">
        function dateDelay(num){
            // 获取当前的时间戳
            var now = new Date();
            // 设置推迟的时间戳
            now.setDate(now.getDate() + num);
            // 获取年
            var year = now.getFullYear();
            // 获取月
            var month = (now.getMonth()+1)<10?"0"+(now.getMonth()+1):(now.getMonth()+1);
            // 获取天
            var day = (now.getDate())<10?"0"+(now.getDate()):(now.getDate());

            return (year+ "-" +month + "-" + day);

        }

        console.log('昨天' + dateDelay(-3));
    </script>

</html>

请使用手机"扫一扫"x

若有恒,何须三更睡五更起;最无益,莫过于一日曝十日寒
原文地址:https://www.cnblogs.com/21haoxingxiu/p/7279660.html