[React Library๐Ÿ“š] Momentjs fromNow(), locale ํ•œ๊ตญ ์‹œ๊ฐ„์œผ๋กœ ์ˆ˜์ •

๋„๋””ยท2020๋…„ 7์›” 22์ผ
4

React Library ๐Ÿ“š

๋ชฉ๋ก ๋ณด๊ธฐ
1/2

์•ˆ๋…•ํ•˜์„ธ์šฉ ๋„๋””์˜ˆ์šฉ
์˜ค๋Š˜์€ momentjs ๊ฐ€์ ธ์™”์–ด์šฉ! ์‹œ๊ฐ„๊ด€๋ จ๋œ ์ž‘์—…์€ momentjs๋ฅผ ์•ˆ ์“ธ์ˆ˜ ์—†์ฃ . ์ €๋„ ์˜ˆ์ „๋ถ€ํ„ฐ ๋งŽ์ด ์จ์™”๋˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ๋ฐ ์˜ค๋Š˜์€ momentjs์—์„œ locale setting์— ๋Œ€ํ•ด ๋งํ•ด๋ณด๋ คํ•ด์š”. momentjs์˜ locale ๊ธฐ๋ณธ๊ฐ’์€ 'en'์ธ๋ฐ ์ด๊ฑธ ํ•œ๊ตญ ๊ฐ’์ธ 'ko'๋กœ ๋ฐ”๊ฟ”์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. fromNow()๋„ ๊ฐ™์ด ์‚ฌ์šฉํ•ด์„œ ์˜ˆ์ œ๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

์ฐธ๊ณ 
momentjs ๋ฌธ์„œ [Time from now]
momentjs ๋ฌธ์„œ [Changing locale global]

locale ์„ธํŒ…ํ•˜๊ธฐ


import moment from 'moment';
console.log(moment().fromNow());

// console.log ๊ฒฐ๊ณผ๊ฐ’
// a few seconds ago

๋‚˜๋Š” a few seconds ago ๋ง๊ณ  ํ•œ๊ธ€๋กœ ๋ช‡์ดˆ์ „์„ ์›ํ•œ๋‹ด๋ง์ด๋‹ค.


import moment from 'moment';
import 'moment/locale/ko'; // ์ด์ค„ ์ถ”๊ฐ€
console.log(moment().fromNow());

// console.log ๊ฒฐ๊ณผ๊ฐ’
// ๋ช‡ ์ดˆ ์ „

import 'moment/locale/ko';

์š”๊ฑฐ ํ•œ์ค„ ์ถ”๊ฐ€ํ•˜๋ฉด locale์ด ko๋กœ ๋ฐ”๋€Œ๊ฒŒ ๋ฉ๋‹ˆ๋‹น! ์˜์ด์ง€

fromNow()


import moment from 'moment';
import 'moment/locale/ko'; // ์ด์ค„ ์ถ”๊ฐ€
console.log(moment([2020, 07, 21, 11, 15]).fromNow());

// console.log ๊ฒฐ๊ณผ๊ฐ’
// ํ•˜๋ฃจ ์ „

๋…„๋„, ์›”, ์ผ, ์‹œ๊ฐ„, ์ดˆ ๋‹จ์œ„๋กœ ๋ฐฐ์—ด์— ๋„ฃ์œผ๋ฉด fromNow()๊ฐ€ ๊ณ„์‚ฐ ๋ฉ๋‹ˆ๋‹ค.
์˜์ด์ง€~~

profile
์ถฉ์ „์ค‘..๐Ÿค”

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2024๋…„ 7์›” 16์ผ

import 'moment/locale/ko'; // ์ด์ค„ ์ถ”๊ฐ€
์ด๊ฒŒ ์–ด๋””์„œ ์ž„ํฌํŠธ ํ•˜๋Š” ๊ฑด๊ฐ€์š”?

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ