개발/Node JS
Node JS - dayjs로 날짜, 시간 관리하기
피로물든딸기
2024. 2. 22. 16:52
반응형
참고
dayjs를 이용하면 날짜나 시간과 관련된 처리를 쉽게할 수 있다.
npm install dayjs
예시 코드는 다음과 같다.
const dayjs = require("dayjs");
// 현재 시간 가져오기
const now = dayjs();
console.log("현재 시간 : ", now.format());
console.log("포맷 변경 : ", now.format("YY-MM-DD / HH:mm:ss"));
console.log("연도 확인 : ", now.get("year")); // or y
console.log("시간 변경 : ", now.set("hour", 0).format()); // or h
// -> year/month/date/day/hour/minute/second/millisecond
console.log("날짜 > 타임 스탬프 : ", now.valueOf());
console.log("타임 스탬프 > 날짜 : ", dayjs(now.valueOf()).format());
const birthday = dayjs("1995-12-25");
const age = now.diff(birthday, "year");
console.log("나이 :", age);
실행 결과는 다음과 같다.
현재 시간 : 2024-02-22T16:49:56+09:00
포맷 변경 : 24-02-22 / 16:49:56
연도 확인 : 2024
시간 변경 : 2024-02-22T00:49:56+09:00
날짜 > 타임 스탬프 : 1708588196122
타임 스탬프 > 날짜 : 2024-02-22T16:49:56+09:00
나이 : 28
반응형