zoukankan      html  css  js  c++  java
  • js获取某个时间段前多长时间

    获取当前时间

      function frontOneHour(fmt) {
          var currentTime = new Date(new Date().getTime())
          // console.log(currentTime) // Wed Jun 20 2018 16:12:12 GMT+0800 (中国标准时间)
          var o = {
            'M+': currentTime.getMonth() + 1, // 月份
            'd+': currentTime.getDate(), //
            'h+': currentTime.getHours(), // 小时
            'm+': currentTime.getMinutes(), //
            's+': currentTime.getSeconds(), //
            'q+': Math.floor((currentTime.getMonth() + 3) / 3), // 季度
            'S': currentTime.getMilliseconds() // 毫秒
          }
          if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (currentTime.getFullYear() + '').substr(4 - RegExp.$1.length))
          for (var k in o) {
            if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : ((
              '00' + o[k]).substr(('' + o[k]).length)))
          }
          return fmt
        }

        console.log(frontOneHour('yyyy-MM-dd hh:mm:ss')) // "2020-12-217 16:43:03"
     

    获取当前时间多长时间之前与多长时间之后

    function add0(m) {
          return m < 10 ? '0' + m : m
        }
        //时间戳转化成时间格式
        function timeFormat(timestamp) {
          //timestamp是整数,否则要parseInt转换,不会出现少个0的情况
          var time = new Date(timestamp);
          var year = time.getFullYear();
          var month = time.getMonth() + 1;
          var date = time.getDate();
          var hours = time.getHours();
          var minutes = time.getMinutes();
          var seconds = time.getSeconds();
          return year + '-' + add0(month) + '-' + add0(date) + ' ' + add0(hours) + ':' + add0(minutes) + ':' + add0(
          seconds);
        }
    
    
    
        // 一、当前时间的前一个小时
        var beforeTime = (new Date().getTime() - 1 * 60 * 10 * 1000);
        console.log('10分钟之前的时间戳:' + beforeTime);
        var endTime = (new Date().getTime() + 1 * 60 * 20 * 1000);
        console.log('-------------------20分钟后时间戳转年月日时分秒' + timeFormat(endTime))
        console.log('-------------------10分钟前时间戳转年月日时分秒' + timeFormat(beforeTime))
  • 相关阅读:
    Python面向对象(组合、菱形继承、多态)
    Python面向对象
    Python hash、xml、configparser、sheve、shutil模块讲解 以及 面向对象初识
    python正则re模块
    Python序列化、date、random、os模块
    Python包和日志模块
    python面向对象、模块讲解
    python递归函数、二分法、匿名函数、(sorted、map、filter内置函数应用)
    Python生成器、三元表达式、列表生成式、字典生成式、生成器表达式
    Python迭代器
  • 原文地址:https://www.cnblogs.com/h5it/p/14150484.html
Copyright © 2011-2022 走看看