본문 바로가기
Frontend/React

[React] timeago 라이브러리

by 민두이 2023. 2. 15.
728x90
반응형

날짜 시간 포맷을 현재 시점 기준으로 아래와 같이 얼마나 전이었는지로 나타내주는 라이브러리

just now
12 seconds ago
2 hours ago
3 days ago
3 weeks ago
2 years ago

in 12 seconds
in 3 minutes
in 24 days
in 6 months

 

  • 다운받기
npm install timeago.js
  • 임포트 해오기
import { format, render, cancel, register } from 'timeago.js';
  • 사용 예시
// format the time with locale
format('2016-06-12', 'en_US');
import {format,register} from 'timeago.js';
import koLocale from 'timeago.js/lib/lang/ko';

register('ko', koLocale);

export function formatAgo(date,lang = 'en_US') {
    return format(date,lang)
}
  • 공식 문서

https://github.com/hustcc/timeago.js/blob/master/README.md

 

GitHub - hustcc/timeago.js: :clock8: timeago.js is a tiny(2.0 kb) library used to format date with `*** time ago` statement.

:clock8: :hourglass: timeago.js is a tiny(2.0 kb) library used to format date with `*** time ago` statement. - GitHub - hustcc/timeago.js: :clock8: timeago.js is a tiny(2.0 kb) library used to form...

github.com

 

728x90