Skip to content

Moment.js

WARNING

Moment를 유지 관리 모드의 레거시 프로젝트로 간주합니다. 시간 관련 라이브러리는 Luxon, Day.js, date-fns, js-Joda 또는 JavaScript:Date, JavaScript:Temporal 를 사용하거나 직접 구현하세요. 관련된 내용은 이곳을 참고. 한글 관련 내용은 moment.js를 사용하면 안 될 이유를 참고.

Parse, validate, manipulate, and display dates and times in JavaScript.

Moment Timezone

Moment의 Timezone 라이브러리. 자세한 내용은 해당 항목 참조.

How to install

npm install moment

타임존 변환후 출력 방법

import moment from 'moment-timezone';

export function iso8601ToLocal(text: string) {
    if (text) {
        return moment(text).tz(moment.tz.guess()).format('LLLL');
    } else {
        return '';
    }
}

export function iso8601ToLocalDate(text?: string) {
    if (text) {
        return moment(text).tz(moment.tz.guess()).format('YYYY-MM-DD');
    } else {
        return '';
    }
}

로케일 변경 방법

moment.locale('ko');

Examples

Format Dates

moment().format('MMMM Do YYYY, h:mm:ss a'); // October 8th 2021, 9:55:17 am
moment().format('dddd');                    // Friday
moment().format("MMM Do YY");               // Oct 8th 21
moment().format('YYYY [escaped] YYYY');     // 2021 escaped 2021
moment().format();                          // 2021-10-08T09:55:17+09:00

Relative Time

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

Calendar Time

moment().subtract(10, 'days').calendar(); // 09/28/2021
moment().subtract(6, 'days').calendar();  // Last Saturday at 9:55 AM
moment().subtract(3, 'days').calendar();  // Last Tuesday at 9:55 AM
moment().subtract(1, 'days').calendar();  // Yesterday at 9:55 AM
moment().calendar();                      // Today at 9:55 AM
moment().add(1, 'days').calendar();       // Tomorrow at 9:55 AM
moment().add(3, 'days').calendar();       // Monday at 9:55 AM
moment().add(10, 'days').calendar();      // 10/18/2021

Multiple Locale Support

moment.locale();         // en
moment().format('LT');   // 9:55 AM
moment().format('LTS');  // 9:55:17 AM
moment().format('L');    // 10/08/2021
moment().format('l');    // 10/8/2021
moment().format('LL');   // October 8, 2021
moment().format('ll');   // Oct 8, 2021
moment().format('LLL');  // October 8, 2021 9:55 AM
moment().format('lll');  // Oct 8, 2021 9:55 AM
moment().format('LLLL'); // Friday, October 8, 2021 9:55 AM
moment().format('llll'); // Fri, Oct 8, 2021 9:55 AM

See also

Favorite site