본문 바로가기
💻CODING/javascript

[js] day.js 사용법 (ft. 날짜, 시간 라이브러리, moment.js)

by 코딩하는 갓디노 2021. 9. 9.

day.js 사용법

 

자바스크립트를 통해 날짜와 시간을 
출력하는 라이브러리,
day.js 입니다.

 

자바스크립트로 날짜와 시간을 출력할 경우,
직접 코드를 작성할 경우도 있지만, 라이브러리를 통해 직접 함수를 만드는 번거로움을 피할 수 있습니다.
과거에는 Moment.js을 많이 사용하였지만, 용량이 너무 많이 차지하여,
최근에는 Moment.js의 경량 버전인 day.js를 많이 사용하고 있습니다.

 

day.js

moment.js의 API 형식은 그대로 유지하면서 용량은 33배 가볍습니다.
사용법이 moment.js와 거의 같다고 보면 되기에 편하게 day.js로 갈아탈 수 있습니다.

 

day.js 사이트

https://www.npmjs.com/package/dayjs

 

dayjs

2KB immutable date time library alternative to Moment.js with the same modern API

www.npmjs.com

 

 

day.js 사용법

설치하기

npm install dayjs --save

 

API 사용한 날짜 출력

import dayjs from 'dayjs';

console.log(dayjs('2021-09-09T18:00:00+09:00').format("YY.MM.DD"))

 

day.js 현재 날짜 구하기

console.log(dayjs().format("YYYY. MM. DD HH:mm:ss"));
//결과
//2021. 09. 09 13:24:52

 

day.js  한국어,  요일 구하기

import dayjs from 'dayjs';
import 'dayjs/locale/ko';

dayjs.locale('ko')
console.log(dayjs().format("YYYY. MM. DD. dddd. HH:mm"))
//결과
//2021. 10. 07. 목요일. 10:07

 

day.js 시간 조작하기

import dayjs from 'dayjs';
import 'dayjs/locale/ko'

 dayjs.locale('ko')
 
 console.log(dayjs().add(30, 'minute').format("A HH:mm"))
 //결과
 //오후 20:30

 

자바스크립트로 직접 현재 날짜와 시간을 구하는 함수는 아래의 포스트로 이동해주세요.

https://goddino.tistory.com/212

 

[js] 현재 날짜, 시간 가져오기(ft. 월, 일, 요일, 시간)

자바스크립트를 이용한 날짜 출력 방법입니다. 현재 날짜, 시간 구하기 let now = new Date(); // 현재 날짜 및 시간 연도 구하기 let year = now.getFullYear(); 월 구하기 let todayMonth = now.getMonth() + 1..

goddino.tistory.com

 

반응형

댓글