欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

javaScript日期工具类DateUtils详解

程序员文章站 2022-04-06 19:17:37
本文实例为大家分享了javascript日期工具类的具体代码,供大家参考,具体内容如下 dateutils = { patterns: {...

本文实例为大家分享了javascript日期工具类的具体代码,供大家参考,具体内容如下

dateutils = { 
    patterns: { 
      pattern_era: 'g', //era 标志符 era strings. for example: "ad" and "bc" 
      pattern_year: 'y', //年 
      pattern_month: 'm', //月份 
      pattern_day_of_month: 'd', //月份的天数 
      pattern_hour_of_day1: 'k', //一天中的小时数(1-24) 
      pattern_hour_of_day0: 'h', //24小时制,一天中的小时数(0-23) 
      pattern_minute: 'm', //小时中的分钟数 
      pattern_second: 's', //秒 
      pattern_millisecond: 's', //毫秒 
      pattern_day_of_week: 'e', //一周中对应的星期,如星期一,周一 
      pattern_day_of_year: 'd', //一年中的第几天 
      pattern_day_of_week_in_month: 'f', //一月中的第几个星期(会把这个月总共过的天数除以7,不够准确,推荐用w) 
      pattern_week_of_year: 'w', //一年中的第几个星期 
      pattern_week_of_month: 'w', //一月中的第几星期(会根据实际情况来算) 
      pattern_am_pm: 'a', //上下午标识 
      pattern_hour1: 'h', //12小时制 ,am/pm 中的小时数(1-12) 
      pattern_hour0: 'k', //和h类型 
      pattern_zone_name: 'z', //时区名 
      pattern_zone_value: 'z', //时区值 
      pattern_week_year: 'y', //和y类型 
      pattern_iso_day_of_week: 'u', 
      pattern_iso_zone: 'x' 
    }, 
    week: { 
      'ch': { 
        "0": "\u65e5", 
        "1": "\u4e00", 
        "2": "\u4e8c", 
        "3": "\u4e09", 
        "4": "\u56db", 
        "5": "\u4e94", 
        "6": "\u516d" 
      }, 
      'en': { 
        "0": "sunday", 
        "1": "monday", 
        "2": "tuesday", 
        "3": "wednesday", 
        "4": "thursday", 
        "5": "friday", 
        "6": "saturday" 
      } 
    }, 
    //获取当前时间 
    getcurrenttime: function() { 
      var today = new date(); 
      var year = today.getfullyear(); 
      var month = today.getmonth() + 1; 
      var day = today.getdate(); 
      var hours = today.gethours(); 
      var minutes = today.getminutes(); 
      var seconds = today.getseconds(); 
      var timestring = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds; 
      return timestring; 
    }, 
    /* 
     * 比较时间大小 
     * time1>time2 return 1 
     * time1<time2 return -1 
     * time1==time2 return 0 
     */ 
    comparetime: function(time1, time2) { 
      if(date.parse(time1.replace(/-/g, "/")) > date.parse(time2.replace(/-/g, "/"))) { 
        return 1; 
      } else if(date.parse(time1.replace(/-/g, "/")) < date.parse(time2.replace(/-/g, "/"))) { 
        return -1; 
      } else if(date.parse(time1.replace(/-/g, "/")) == date.parse(time2.replace(/-/g, "/"))) { 
        return 0; 
      } 
    }, 
    //是否闰年 
    isleapyear: function(year) { 
      return((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0); 
    }, 
    //获取某个月的天数,从0开始 
    getdaysofmonth: function(year, month) { 
      return [31, (this.isleapyear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]; 
    }, 
    getdaysofmonth2: function(year, month) { 
      // 将天置为0,会获取其上个月的最后一天 
      month = parseint(month) + 1; 
      var date = new date(year, month, 0); 
      return date.getdate(); 
    }, 
    /*距离现在几天的日期:负数表示今天之前的日期,0表示今天,整数表示未来的日期 
     * 如-1表示昨天的日期,0表示今天,2表示后天 
     */ 
    fromtoday: function(days) { 
      var today = new date(); 
      today.setdate(today.getdate() + days); 
      var date = today.getfullyear() + "-" + (today.getmonth() + 1) + "-" + today.getdate(); 
      return date; 
    }, 
    /** 
     * 日期时间格式化 
     * @param {object} datetime 需要格式化的日期时间 
     * @param {string} pattern 格式化的模式,如yyyy-mm-dd hh(hh):mm:ss.s a k k e d f w w z z 
     */ 
    formt: function(datetime, pattern) { 
      var date = new date(datetime); 
      if(bee.stringutils.isblank(pattern)) { 
        return date.tolocalestring(); 
      } 
      return pattern.replace(/([a-z])\1*/ig, function(matchstr, group1) { 
        var replacement = ""; 
        switch(group1) { 
          case bee.dateutils.patterns.pattern_era: //g 
            break; 
          case bee.dateutils.patterns.pattern_week_year: //y 
          case bee.dateutils.patterns.pattern_year: //y 
            replacement = date.getfullyear(); 
            break; 
          case bee.dateutils.patterns.pattern_month: //m 
            var month = date.getmonth() + 1; 
            replacement = (month < 10 && matchstr.length >= 2) ? "0" + month : month; 
            break; 
          case bee.dateutils.patterns.pattern_day_of_month: //d 
            var days = date.getdate(); 
            replacement = (days < 10 && matchstr.length >= 2) ? "0" + days : days; 
            break; 
          case bee.dateutils.patterns.pattern_hour_of_day1: //k(1~24) 
            var hours24 = date.gethours(); 
            replacement = hours24; 
            break; 
          case bee.dateutils.patterns.pattern_hour_of_day0: //h(0~23) 
            var hours24 = date.gethours(); 
            replacement = (hours24 < 10 && matchstr.length >= 2) ? "0" + hours24 : hours24; 
            break; 
          case bee.dateutils.patterns.pattern_minute: //m 
            var minutes = date.getminutes(); 
            replacement = (minutes < 10 && matchstr.length >= 2) ? "0" + minutes : minutes; 
            break; 
          case bee.dateutils.patterns.pattern_second: //s 
            var seconds = date.getseconds(); 
            replacement = (seconds < 10 && matchstr.length >= 2) ? "0" + seconds : seconds; 
            break; 
          case bee.dateutils.patterns.pattern_millisecond: //s 
            var milliseconds = date.getmilliseconds(); 
            replacement = milliseconds; 
            break; 
          case bee.dateutils.patterns.pattern_day_of_week: //e 
            var day = date.getday(); 
            replacement = bee.dateutils.week['ch'][day]; 
            break; 
          case bee.dateutils.patterns.pattern_day_of_year: //d 
            replacement = bee.dateutils.dayoftheyear(date); 
            break; 
          case bee.dateutils.patterns.pattern_day_of_week_in_month: //f 
            var days = date.getdate(); 
            replacement = math.floor(days / 7); 
            break; 
          case bee.dateutils.patterns.pattern_week_of_year: //w 
            var days = bee.dateutils.dayoftheyear(date); 
            replacement = math.ceil(days / 7); 
            break; 
          case bee.dateutils.patterns.pattern_week_of_month: //w 
            var days = date.getdate(); 
            replacement = math.ceil(days / 7); 
            break; 
          case bee.dateutils.patterns.pattern_am_pm: //a 
            var hours24 = date.gethours(); 
            replacement = hours24 < 12 ? "\u4e0a\u5348" : "\u4e0b\u5348"; 
            break; 
          case bee.dateutils.patterns.pattern_hour1: //h(1~12) 
            var hours12 = date.gethours() % 12 || 12; //0转为12 
            replacement = (hours12 < 10 && matchstr.length >= 2) ? "0" + hours12 : hours12; 
            break; 
          case bee.dateutils.patterns.pattern_hour0: //k(0~11) 
            var hours12 = date.gethours() % 12; 
            replacement = hours12; 
            break; 
          case bee.dateutils.patterns.pattern_zone_name: //z 
            replacement = bee.dateutils.getzonenamevalue(date)['name']; 
            break; 
          case bee.dateutils.patterns.pattern_zone_value: //z 
            replacement = bee.dateutils.getzonenamevalue(date)['value']; 
            break; 
          case bee.dateutils.patterns.pattern_iso_day_of_week: //u 
            break; 
          case bee.dateutils.patterns.pattern_iso_zone: //x 
            break; 
          default: 
            break; 
        } 
        return replacement; 
      }); 
    }, 
    /** 
     * 计算一个日期是当年的第几天 
     * @param {object} date 
     */ 
    dayoftheyear: function(date) { 
      var obj = new date(date); 
      var year = obj.getfullyear(); 
      var month = obj.getmonth(); //从0开始 
      var days = obj.getdate(); 
      var daysarr = [31, (this.isleapyear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 
      for(var i = 0; i < month; i++) { 
        days += daysarr[i]; 
      } 
      return days; 
    }, 
    //获得时区名和值 
    getzonenamevalue: function(dateobj) { 
      var date = new date(dateobj); 
      date = new date(date.utc(date.getfullyear(), date.getmonth(), date.getdate())); 
      var arr = date.tostring().match(/([a-z]+)([-+]\d+:?\d+)/); 
      var obj = { 
        'name': arr[1], 
        'value': arr[2] 
      }; 
      return obj; 
    } 
  }; 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。