zoukankan      html  css  js  c++  java
  • js获取当天时间,7天前后时间,时间格式化

    格式化时间年月日时分秒

    //时间戳转换方法    date:时间戳数字
    formatDate(date) {
      var date = new Date(date);
      var YY = date.getFullYear() + '-';
      var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
      var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
      var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
      var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
      var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
      return YY + MM + DD +" "+hh + mm + ss;
    },
    

      

      格式化时间年月日

        $dateFormatDay=(timestamp)=>{
              var date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
              var Y = date.getFullYear() + '-';
              var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
              var D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' ';
              return Y + M + D ;
            }
    

      获取当天凌晨时间

     startTime = new Date(new Date().setHours(0, 0, 0, 0));
     startTime = $dateFormat(startTime);
    

      获取7天前时间

     var SevenDayAgo = startTime - 86400 * 7 * 1000;
     SevenDayAgo = $dateFormat(SevenDayAgo);
    

      获取7天后时间

    var SevenDayLater = startTime*1 + 86400 * 7 * 1000;
    SevenDayLater = $dateFormat(SevenDayLater);
    

      获取1个月前时间

    var AmonthAgo= startTime.setMonth(startTime.getMonth() - 1);
    AmonthAgo = $dateFormat(AmonthAgo);
    

      

  • 相关阅读:
    前端之HTML
    面向对象编程思想
    【统计】Causal Inference
    Python 最常见的 170 道面试题全解析:2019 版
    面试mysql表设计要注意啥
    数据分析资料汇总
    【crunch bang】论坛tint2配置讨论贴
    【crunch bang】增加壁纸图片文件
    【linux】xx is not in the sudoers file 解决办法
    【crunch bang】安装firefox,删除iceweasel
  • 原文地址:https://www.cnblogs.com/gitByLegend/p/11052291.html
Copyright © 2011-2022 走看看