js获取当前日期和前后“N天”日期,严谨版

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>

<style type="text/css">
    body{text-align:center} 
</style>

<script type="text/javascript">
    
//设置所有url;
function setallurl(){
    
    //调用日期js
    var infoTime = getNowDate();
    
    //当前时间
    var endTime = infoTime["endTime"];
    //当前时间-前一个月
    var startTime = infoTime["startTime"];
    
    
    document.getElementById("endTime").value=endTime;
    document.getElementById("startTime").value=startTime;
    
}


//日期获取
function getNowDate() {
    
     var sign1 = "-";
     var sign2 = ":";
     //当前日期
     var date = new Date();
     var nowYear = date.getFullYear() // 年
     var nowMonth = date.getMonth() + 1; // 月
     var nowDay  = date.getDate(); // 日
     var nowHour = date.getHours(); // 时
     var nowMinutes = date.getMinutes(); // 分
     var nowSeconds = date.getSeconds() // 秒
     
     // 给一位数数据前面补加“0”
     if (nowMonth < 10) {
         nowMonth = "0" + nowMonth;
     }
     if (nowDay < 10) {
          nowDay = "0" + nowDay;
     }
     if (nowHour < 10) {
          nowHour = "0" + nowHour;
     }
     if (nowMinutes < 10) {
           nowMinutes = "0" + nowMinutes;
     }
     if (nowSeconds < 10) {
          nowSeconds = "0" + nowSeconds;
     }
     
     //前10天
     var pastTime = new Date(date - 1000 * 60 * 60 * 24 * 10);//最后“10”数字根据需求修改,代表10天
     //var pastTime = new Date(date + 1000 * 60 * 60 * 24 * 10);//“+”表示当前日期后“10”天
     var pastYear = pastTime.getFullYear() // 年
     var pastMonth = pastTime.getMonth() + 1; // 月
     var pastDay  = pastTime.getDate(); // 日
     var pastHour = pastTime.getHours(); // 时
     var pastMinutes = pastTime.getMinutes(); // 分
     var pastSeconds = pastTime.getSeconds() // 秒
     
     // 给一位数数据前面补加“0”
     if (pastMonth < 10) {
         pastMonth = "0" + pastMonth;
     }
     if (pastDay < 10) {
          pastDay = "0" + pastDay;
     }
     if (pastHour < 10) {
          pastHour = "0" + pastHour;
     }
     if (pastMinutes < 10) {
           pastMinutes = "0" + pastMinutes;
     }
     if (pastSeconds < 10) {
          pastSeconds = "0" + pastSeconds;
     }
     
     //当前日期
     var endTime = nowYear + sign1 + nowMonth + sign1 + nowDay + " " + nowHour + sign2 + nowMinutes + sign2 + nowSeconds;
     //前一个月日期
     var startTime = pastYear + sign1 + pastMonth + sign1 + pastDay + " " + pastHour + sign2 + pastMinutes + sign2 + pastSeconds;
     
     var infoTime = {"startTime":startTime,"endTime":endTime};
     
     return infoTime;
}

</script>
</head>
<body οnlοad="setallurl()">
        
      前一个月:<input id="startTime" type="text" value="" /></br>
        当前日期:<input id="endTime" type="text" value="" />
        
</body>
</html>

结果展示:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值