Rianshin

[Javascript]Date, Time formatter 본문

Develop/Javascript

[Javascript]Date, Time formatter

RianShin 2020. 11. 25. 15:52
728x90
반응형
SMALL
formatter: function () {
      var date = new Date();
      var dateFormatter = function dateFormatter(date, format) {
        try {
          var arr = [];
          var seperator = [];

          if (date === undefined || date === null) {
            return null;
          }
          if (typeof date === 'string') {
            arr = date.split(/\D/),
            seperator = ['', '', ''];
            if (arr.length === 1) {// 월일년을 따로 넣는다.
              arr = [date.substr(0, 4), date.substr(4, 2), date.substr(-2)];
            }
          } else if (_typeof(date) === 'object') {// instanceof 는 안될거 같아서 string or date로만 넣자
            var year = date.getFullYear(),month = date.getMonth() + 1,day = date.getDate();
            arr.push(year);
            arr.push(month < 10 ? '0' + '' + month : month);
            arr.push(day < 10 ? '0' + day : day);
          }

          switch (format.toLowerCase()) {
            case 'array':
              return arr;
            case 'yyyymmdd':
              seperator = ['', '', ''];
              break;
            case 'yyyy-mm-dd':
              seperator = ['-', '-', ''];
              break;
            case 'yyyy,mm,dd':
              seperator = [',', ',', ''];
              break;
            case 'yyyy.mm.dd':
              seperator = ['.', '.', ''];
              break;
            case 'mm/dd/yyyy':
            case 'en':
              seperator = ['/', '/', '/'];
              var str = arr[1] + seperator[0] + arr[2] + seperator[1] + arr[0] + seperator[2];
              return str;
            case 'ko': //navigator 에 있는 언어설정값이랑 동일 @@@@
            case 'ko-kr':
              seperator = ['년', '월', '일'];
              break;}

          var str = arr[0] + seperator[0] + arr[1] + seperator[1] + arr[2] + seperator[2];
          return str;
        } catch (e) {
          throw new JsExecutionErrror('utils - dateFormatter');
        }
      };
      var timeFormatter = function timeFormatter(time, format) {
        try {
          var arr = [];
          var seperator = [];
          if (!time) {
            return '';
          }
          if (typeof time === 'string') {
            arr = time.split(/\D/),
            seperator = ['', ''];
            if (arr.length === 1) {// 시분을 분리하자
              arr = [time.substr(0, 2), time.substr(2, 2)];
            }
          } else if (_typeof(time) === 'object') {// object로 오면 date 라고보자.. instanceof 는 안될 수 있으니까..
            var hours = time.getHours().toString(),min = time.getMinutes().toString(),
            sec = time.getSeconds().toString();
            arr = [hours.length < 2 ? '0' + hours : hours, min.length < 2 ? '0' + min : min, sec.length < 2 ? '0' + sec : sec];
          }

          switch (format.toLowerCase()) {
            case 'array':
              return arr;
            case 'hhmmss':
              return arr.join('');
            case 'hhmm':
              seperator = ['', ''];
              break;
            case 'hh:mm':
            case 'en':
              seperator = [':', ''];
              break;
            case 'ko': //navigator 에 있는 언어설정값이랑 동일 @@@@
            case 'ko-kr':
              seperator = ['시', '분'];
              break;}

          var str = arr[0].toString() + seperator[0] + arr[1].toString() + seperator[1];
          return str;
        } catch (e) {
          throw new JsExecutionErrror('utils - timeFormatter');
        }
      };

      return {
        date: function date(_date, format) {
          return dateFormatter(_date, format);
        },
        time: function time(date, format) {
          return timeFormatter(date, format);
        },
        datetime: function datetime(date, format) {
          return dateFormatter(date, format) + timeFormatter(time, format);
        } };

    }
728x90
반응형
LIST

'Develop > Javascript' 카테고리의 다른 글

[Javascript] iOS판별  (0) 2020.11.25
[Javascript] Android 판별  (0) 2020.11.25
[JS] jQuery 특정위치로 스크롤  (0) 2020.11.20
[JavaScript] 소수점처리 Math  (0) 2020.11.11
[jQuery] Ajax JSON POST in HTTP Body  (0) 2020.11.03
Comments