Translate

[Angular 2+] Java의 ZonedDateTime 형식 Formatting





환경 - Angular 4



다국어 홈페이지를 제작하려다보니 DateTime 형식을
Java 8의 ZonedDateTime을 사용하게 되었다.


1004lucifer
아래와 같은 포멧을 사용한다. (ISO 8601)

- 2017-11-14T12:52:23+00:00
- 2017-11-14T12:52:23Z
- 20171114T125223Z



앵귤러에서 지원하는게 있을까 한참 찾다가 포멧을 직접 만들까 했는데
ES6 에서 기본 지원이 되고 있었다.






ES6에서 아래와 같이 사용 가능하다 (TypeScript 사용가능)
var today = new Date('05 October 2011 14:48 UTC');

console.log(today.toISOString()); // Returns 2011-10-05T14:48:00.000Z

1004lucifer

ES5의 JavaScript 에서는 아래와 같이 prototype 추가해서 사용 가능하다.
if (!Date.prototype.toISOString) {
  (function() {

    function pad(number) {
      if (number < 10) {
        return '0' + number;
      }
      return number;
    }

    Date.prototype.toISOString = function() {
      return this.getUTCFullYear() +
        '-' + pad(this.getUTCMonth() + 1) +
        '-' + pad(this.getUTCDate()) +
        'T' + pad(this.getUTCHours()) +
        ':' + pad(this.getUTCMinutes()) +
        ':' + pad(this.getUTCSeconds()) +
        '.' + (this.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) +
        'Z';
    };

  }());
}




참고
- http://doohyun.tistory.com/55
- https://stackoverflow.com/questions/29297257/what-time-zone-does-the-javascript-new-date-use
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString

댓글