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);
    

      

  • 相关阅读:
    开源的Ajax.Net类库
    开放的即时通信协议Jabber
    代码生成工具之MyGeneration
    清华梦的粉碎—写给清华大学的退学申请(牛人 王垠 )
    [.NET网格计算框架] Alchemi
    .net安全类库
    使用Facility:EnterpriseLibrary整合进Castle
    castle project 发布新版本
    10招步骤保护IIS服务器安全
    应用IBatisNet+Castle进行项目的开发
  • 原文地址:https://www.cnblogs.com/gitByLegend/p/11052291.html
Copyright © 2011-2022 走看看