javascript 날짜 도구 류 DateUtils 상세 설명

본 논문 의 사례 는 자바 script 날짜 도구 류 의 구체 적 인 코드 를 공유 하 였 으 며,구체 적 인 내용 은 다음 과 같 습 니 다.

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; 
    } 
  }; 
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기