본문 바로가기

Development/Javascript

[Javascript] moment.js를 이용한 timezone Datetime

반응형

우선 moment.js 를 참조 한다.

moment.js

자바스크립트로 날짜와 시간을 다루는 것의 번거로움을 줄여 Library 다.

https://momentjs.com/

 

Moment.js | Home

Format Dates moment().format('MMMM Do YYYY, h:mm:ss a'); moment().format('dddd'); moment().format("MMM Do YY"); moment().format('YYYY [escaped] YYYY'); moment().format(); Relative Time moment("20111031", "YYYYMMDD").fromNow(); moment("20120620", "YYYYMMDD"

momentjs.com

보통 cdn 에 링크를 걸어 사용한다.

// moment.js cdn
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment-with-locales.min.js"></script>
// timezone cdn
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.34/moment-timezone-with-data-10-year-range.js"></script>

https://cdnjs.com/libraries/moment.js

 

moment.js - Libraries - cdnjs - The #1 free and open source CDN built to make life easier for developers

Parse, validate, manipulate, and display dates - Simple. Fast. Reliable. Content delivery at its finest. cdnjs is a free and open-source CDN service trusted by over 12.5% of all websites, serving over 200 billion requests each month, powered by Cloudflare.

cdnjs.com

참고로 google apps script 에서는 아래와 같이 참조 한다.

// moment.js cdn
eval(UrlFetchApp.fetch('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment-with-locales.min.js').getContentText());
// timezone cdn
eval(UrlFetchApp.fetch('https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.34/moment-timezone-with-data-10-year-range.js').getContentText());

Google Apps Script 파일 참조

사실 본 라이브러리를 참조 하게 된게 Google Apps Script 에서 local 시간을 보니 서버에 미국 동부에 있는 것인지 시간이 다르기에 참조 했다.

function getLocalDatetime()
{
   var localDT = new moment().tz("Asia/Seoul");
   Logger.log(localDT.format('yyyy-MM-DD'));
   Logger.log(localDT.format('HH:mm:ss'));

}
반응형