ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Moment
    공부/JavaScript 2021. 7. 1. 17:12

    Moment

    : 날짜, 및 시간을 다루는 라이브러리


    설치

    • npm i moment

    사용법

    • const moment = require('moment')

    format()

    : 원하는 포맷을 지정하여 출력할수 있다. 대소문자 주의 할 것

    년 월 일

    현재 시간

    • const now = moment().format()

    년(Year)

    • YYYY (4자리 연도) / YY (2자리 연도) / Y (4자리 축약)
    • const year1 = moment().format('YYYY YY Y')

    월(Month)

    • MMMM (월 이름) / MMM (월 이름 축약)
    • MM (월) / M (0제외 월) : 1~12
    • Q (해당 월의 분기) : 1~4
    • const now1 = moment().format('MMMM MMM')
      const now2 = moment().format('MM M')
      const now3 = moment().format('Q')

    일(Day)

    • DDDD (년도의 일) / DDD (년도의 일 축약) 1~365
    • DD (달의 일) / D (0제외 달의 일) 1~31
    • Do (서 수로 표현) 1st...31st
    • const now1 = moment().format('DDDD DDD')
      const now2 = moment().format('DD D')
      const now3 = moment().format('Do')

    시 분 초

    시 (Hours)

    • HH (24시간) / H (0 제외 24시간) 0~23
    • hh (12시간) / h (0 제외 12시간) 1~12
    • kk (24시간) / k (0 제외 24시간) 1~24
    • A (포스트 대문자) AM PM / a (포스트 소문자) am pm
    • ZZ (UTC 표기) +0900 / zz (UTC 표기) +09:00
    • const now1 = moment().format('HH H')
      const now2 = moment().format('hh h')
      const now3 = moment().format('kk k')
      const now4 = moment().format('a A')
      const now5 = moment().format('ZZ Z')

    분 (Minutes)

    • mm (분) / m (0 제외 분) 0~59
    • const now1 = moment().format('mm m')

    초 (Seconds)

    • ss (초) / s (0 제외 초) 0~59
    • SSS SS S 0~999
    • const now1 = moment().format('ss s')
      const now2 = moment().format('SSS SS S')

    주별 / 주중

    년도 (Year)

    • gggg (4자리 년도) / gg(2자리 년도)
    • const now1 = moment().format('gggg gg')

    주 (Week)

    • ww (주) / w (0 제외 주) 1~53
    • const now2 = moment().format('ww w')

    일 (Day)

    • e (요일) 0~6
    • dddd /ddd /ddd (요일 이름)
    • const now3 = moment().format('e')
      const now4 = moment().format('dddd ddd dd')

    ISO 표기법

    년도 (Year)

    • GGGG (4자리 년도) / GG (2자리 년도)
    • const now1 = moment().format('GGGG GG')

    주 (Week)

    • WW (주) / W (0 제외 주) 1~53
    • const now2 = moment().format('WW W')

    일 (Day)

    • E (요일) 1~7
    • const now3 = moment().format('E')

    지역화 (locale)

    : 해당 시간을 문자열로 반환

    • L (날짜 현지 방식)

    • LL (날짜 현지 방식)

    • LLL (날짜 현지 방식, 시간, 포스트)

    • LLLL (날짜 현지 방식, 시간, 포스트, 요일)

    • LT (시, 분, 포스트)

    • LTS (시, 분, 초, 포스트)

    • const now1 = moment().format('L')
      const now2 = moment().format('LL')
      const now3 = moment().format('LLL')
      const now4 = moment().format('LLLL')
      const now5 = moment().format('LT')
      const now6 = moment().format('LTS')

    단위 별 출력

    년 월 일 요일

    • 월은 0~11 을 반환 -> +1을 해준다
    • const now1 = moment().year()
      const now2 = moment().month() + 1
      const now3 = moment().date()
      const now4 = momnet().day()

    시 분 초 밀리초

    • const now1 = moment().hours()
      const now2 = moment().minutes()
      const now3 = moment().seconds()
      const now4 = moment().milliseconds()

    달력 형식

    • const now1 = moment().calendar()

    날짜 계산

    날짜 더하기

    add(value, key)

    • moment().add() 는 moment 를 반환하므로 원하는 형식을 지정해서 출력한다
    • // 날짜
      const now1 = moment().add(7, 'y').format('LLLL')
      const now2 = moment().add(7, 'Q').format('LLLL')
      const now3 = moment().add(7, 'd').format('LLLL')
      const now4 = moment().add(7, 'D').format('LLLL')
      const now5 = moment().add(7, 'w').format('LLLL')
      // 시간
      const now6 = moment().add(7, 'h').format('HH:mm:ss')
      const now7 = moment().add(7, 'm').format('HH:mm:ss')
      const now8 = moment().add(7, 's').format('HH:mm:ss')
      const now9 = moment().add(7, 'ms').format('HH:mm:ss')

    날짜 빼기

    : add() 함수에 음수로 계산해도 된다

    subtract(value, key)

    • // 날짜
      const now1 = moment().subtract(7, 'y').format('LLLL')
      const now2 = moment().subtract(7, 'Q').format('LLLL')
      const now3 = moment().subtract(7, 'd').format('LLLL')
      const now4 = moment().subtract(7, 'D').format('LLLL')
      const now5 = moment().subtract(7, 'w').format('LLLL')
      // 시간
      const now6 = moment().subtract(7, 'h').format('HH:mm:ss')
      const now7 = moment().subtract(7, 'm').format('HH:mm:ss')
      const now8 = moment().subtract(7, 's').format('HH:mm:ss')
      const now9 = moment().subtract(7, 'ms').format('HH:mm:ss')

    key 값 정리

    Key Shorthand
    years y
    quarterts Q
    months M
    weeks w
    days d
    hours h
    minutes m
    seconds s
    milliseconds ms

    특정 날짜로부터 계산

    moment(비교대상).diff(기준 일, 반환 타입)

    • 2022년 1월 1일 까지 계산
    • const now1 = moment('20220101').diff(moment(), 'y')
      const now2 = moment('20220101').diff(moment(), 'M')
      const now3 = moment('20220101').diff(moment(), 'd')
      const now4 = moment('20220101').diff(moment(), 'h')
      const now5 = moment('20220101').diff(moment(), 'm')
      const now6 = moment('20220101').diff(moment(), 's')
      const now7 = moment('20220101').diff(moment(), 'ms')

    fromNow() / from()

    • 현재부터 기준날짜까지 계산
    • const now1 = moment('20220101').fromNow()
    • 두 날짜 사이의 계산
    • const now2 = moment('20210701') 
      const now3 = moment('20220701') 
      console.log(now2.from(now3)

    현재로부터 계산

    • 오늘 지나간 시간
    • 오늘 남은 시간
    • 시간 내 지나간 분
    • const now1 = moment().startOf('d').fromNow()
      const now2 = moment().endOf('d').fromNow()
      const now3 = moment().startOf('h').fromNow()
    반응형

    '공부 > JavaScript' 카테고리의 다른 글

    Promise  (0) 2021.06.21
    비동기, 콜백  (0) 2021.06.21

    댓글

Designed by Tistory.