[Javascript] moment.js 라이브러리 사용법

Yuri Lee·2022년 4월 17일
0

Intro

날짜나 시간을 손쉽게 사용할 수 있게 도와주는 라이브러리인 moment.js에 대해 알아보도록 하자.

Install

npm install moment --save   # npm
yarn add moment             # Yarn
Install-Package Moment.js   # NuGet
spm install moment --save   # spm
meteor add momentjs:moment  # meteor
bower install moment --save # bower (deprecated)

여러 shell 명령을 통해 설치할 수 있다. 각자 설치되어있는 패키지 관리 툴을 사용하여 진행하면 될 것 같다.

How to use

Format Dates

moment().format('MMMM Do YYYY, h:mm:ss a'); // April 18th 2022, 8:56:48 am
moment().format('dddd');                    // Monday
moment().format("MMM Do YY");               // Apr 18th 22
moment().format('YYYY [escaped] YYYY');     // 2022 escaped 2022
moment().format();                         

Relative Time

moment("20111031", "YYYYMMDD").fromNow(); // 10 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 10 years ago
moment().startOf('day').fromNow();        // 9 hours ago
moment().endOf('day').fromNow();          // in 15 hours
moment().startOf('hour').fromNow();      

Calendar Time

moment().subtract(10, 'days').calendar(); // 04/08/2022
moment().subtract(6, 'days').calendar();  // Last Tuesday at 8:57 AM
moment().subtract(3, 'days').calendar();  // Last Friday at 8:57 AM
moment().subtract(1, 'days').calendar();  // Yesterday at 8:57 AM
moment().calendar();                      // Today at 8:57 AM
moment().add(1, 'days').calendar();       // Tomorrow at 8:57 AM
moment().add(3, 'days').calendar();       // Thursday at 8:57 AM
moment().add(10, 'days').calendar();     

Multiple Locale Support

moment.locale();         // en
moment().format('LT');   // 8:57 AM
moment().format('LTS');  // 8:57:41 AM
moment().format('L');    // 04/18/2022
moment().format('l');    // 4/18/2022
moment().format('LL');   // April 18, 2022
moment().format('ll');   // Apr 18, 2022
moment().format('LLL');  // April 18, 2022 8:57 AM
moment().format('lll');  // Apr 18, 2022 8:57 AM
moment().format('LLLL'); // Monday, April 18, 2022 8:57 AM
moment().format('llll');

이와 같이 다양한 방식으로 날짜 포멧을 활용할 수 있다. 필요 목적에 따라 사용하면 된다.


https://momentjs.com/

profile
Step by step goes a long way ✨

0개의 댓글