dayjs

bunny.log·2022년 10월 20일
0
npm install dayjs

자주쓰는 포멧

dayjs(new Date()).format('YY.MM.DD')

현재 날짜 및 시간 객체 생성 날짜 및 시간 지정 - dayjs()

포맷 지정

  date.format(); // 2022-05-24T10:30:25+09:00
  date.format("YY-MM-DD"); // 22-05-24
  date.format("DD/MM/YY"); // 05/24/22
  date.format("YYYY.MM.DD HH:mm:ss"); // 2022.05.24 10:30:25
  date.format("YYYY년MM월DD일 HH:mm:ss") // 2022년05월24일 10:30:25
  

요일

dayjs.locale('ko');
dayjs(2022-11-13T18:08:33).format('MM.DD(ddd)') // 11.13(월)

날짜 시간 단위 값 구하기 - get()

const now = dayjs();

  now.format(); // 2022-05-24T19:03:02+09:00

  now.get("year"); // 2022 (년)
  now.get("y"); // 2022 (년)

  now.get("month"); // 05 (월 - 0~11)
  now.get("M"); // 5 (월 - 0~11)

  now.get("date"); // 24 (일)
  now.get("D"); // 24 (일)

  now.get("day"); // 0 (요일 - 일요일 : 0, 토요일 : 6)
  now.get("d"); // 0 (요일 - 일요일 : 0, 토요일 : 6)

  now.get("hour"); // 19 (시)
  now.get("h"); // 19 (시)

  now.get("minute"); // 3 (분)
  now.get("m"); // 3 (분)

  now.get("second"); // 2 (초)
  now.get("s"); // 2 (초)

  now.get("millisecond"); // 179 (밀리초)
  now.get("ms"); // 179 (밀리초)

날짜 및 시간 더하기 빼기 - add(), subtract()

const date = dayjs("2022-05-24 10:30:21");
  date.add(1, "year").format(); // 2023
  date.add(1, "month").format(); // 2022-06
  date.add(1, "week").format(); // 2022-05-31

  date.add(1, "year").format(); // 2021
  date.add(1, "month").format(); // 2022-04
  date.add(1, "week").format(); // 2022-05-17

day.js에서 지원하는 함수 일부를 기록 해봤다.
https://day.js.org/en/
더 자세한 사항은 공식문서를 읽어보시길 바랍니다.

참고
https://velog.io/@silent10z/Dayjs-%EC%82%AC%EC%9A%A9%EB%B2%95

profile
나를 위한 경험기록

0개의 댓글