美文网首页
合并对象 时间格式化 判断是否今天 差几天,过去几天

合并对象 时间格式化 判断是否今天 差几天,过去几天

作者: 凡凡的小web | 来源:发表于2020-07-08 14:33 被阅读0次
    // https://segmentfault.com/q/1010000005018933#answer-1020000005019144
    if (!Object.assign) {
      Object.defineProperty(Object, "assign", {
        enumerable: false,
        configurable: true,
        writable: true,
        value: function(target, firstSource) {
          "use strict";
          if (target === undefined || target === null)
            throw new TypeError("Cannot convert first argument to object");
          var to = Object(target);
          for (var i = 1; i < arguments.length; i++) {
            var nextSource = arguments[i];
            if (nextSource === undefined || nextSource === null) continue;
            var keysArray = Object.keys(Object(nextSource));
            for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
              var nextKey = keysArray[nextIndex];
              var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
              if (desc !== undefined && desc.enumerable) to[nextKey] = nextSource[nextKey];
            }
          }
          return to;
        }
      });
    }
    
    Number.prototype.formatTime = function () {
      // 计算
      var i = 0,
          s = parseInt(this)
      if (s > 60) {
          i = parseInt(s / 60)
          s = parseInt(s % 60)
      }
      // 补零
      var zero = function (v) {
          return v >> 0 < 10 ? '0' + v : v
      }
      return [zero(i), zero(s)].join(':')
    }
    
    [https://www.cnblogs.com/hwaggLee/p/4651892.html](https://www.cnblogs.com/hwaggLee/p/4651892.html)
    
    Date.prototype.format = function(format){ 
      var o = { 
      "M+" : this.getMonth()+1, //month 
      "d+" : this.getDate(), //day 
      "h+" : this.getHours(), //hour 
      "m+" : this.getMinutes(), //minute 
      "s+" : this.getSeconds(), //second 
      "q+" : Math.floor((this.getMonth()+3)/3), //quarter 
      "S" : this.getMilliseconds() //millisecond 
      } 
    
      if(/(y+)/.test(format)) { 
      format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
      } 
      var week = {         
          "0" : "一",         
          "1" : "二",         
          "2" : "三",         
          "3" : "四",         
          "4" : "五",         
          "5" : "六",         
          "6" : "日"        
          };  
      if(/(E+)/.test(format)){         
          format=format.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "星期" : "周") : "")+week[this.getDay()+""]);         
      }    
      
      for(var k in o) { 
      if(new RegExp("("+ k +")").test(format)) { 
      format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); 
      } 
      } 
      return format; 
      }
    // 使用new Date(time).format("MM月dd日 EE")
    // 使用new Date(time).format("yyyy年MM月dd日 EEE")
    
    # [js判断时间是否为今天日期(判断日期与当前日期相差多少天)](https://www.cnblogs.com/qiuxiaojian/p/9403215.html)
    
    下面是我的代码:
    
    

    // 判断时间为今天
    judgeTime(data){
    var date = data.toString();
    var year = date.substring(0, 4);
    var month = date.substring(4, 6);
    var day = date.substring(6, 8);
    var d1 = new Date(year + '/' + month + '/' + day);
    var dd = new Date();
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1;
    var d = dd.getDate();
    var d2 = new Date(y + '/' + m + '/' + d);
    var iday = parseInt(d2 - d1) / 1000 / 60 / 60 / 24;
    return iday;
    },

    var start_time="2018-07-31T14:00" //这是一般的时间格式,下面是使用replace方法进行转换为数字格
    //式

    if(judgeTime(start_time.replace(/-|T|:/g, ''))==0){
    console.log('日期为今天')
    }

    
    如果数字为负数,-1为明天   负多少就是差多少    正数相反的道理
    
    

    相关文章

      网友评论

          本文标题:合并对象 时间格式化 判断是否今天 差几天,过去几天

          本文链接:https://www.haomeiwen.com/subject/yuhccktx.html