Front/JS & jQuery
[js] moment().toDate()
์ค์ ์งโฌ
2025. 3. 28. 20:09
728x90
๋ฐ์ํ
๐ moment().toDate()๋?
moment().toDate()๋ Moment.js์์ ์ฌ์ฉํ๋ ๋ฉ์๋๋ก,
moment ๊ฐ์ฒด๋ฅผ JavaScript Date ๊ฐ์ฒด๋ก ๋ณํํ๋ ๊ธฐ๋ฅ์ ํฉ๋๋ค.
โ ์ toDate()๋ฅผ ์ฌ์ฉํ ๊น?
moment()๋ก ์์ฑํ ๋ ์ง๋ Moment.js์ ๊ฐ์ฒด์ด๊ณ ,
datepicker๋ ์ผ๋ฐ์ ์ธ JavaScript Date ํจ์๋ Date ๊ฐ์ฒด๋ฅผ ํ์๋ก ํฉ๋๋ค.
๐ ๋ฐ๋ผ์ moment().toDate()๋ฅผ ์ฌ์ฉํ์ฌ Moment ๊ฐ์ฒด๋ฅผ JavaScript Date ๊ฐ์ฒด๋ก ๋ณํํ๋ ๊ฒ์ ๋๋ค.
๐ ์ฌ์ฉ ์์
๐ฏ 1๏ธโฃ Moment.js์ toDate()๋ฅผ ์ฌ์ฉํ ์
let momentDate = moment(); // Moment ๊ฐ์ฒด
console.log(momentDate); // ๐ Moment ๊ฐ์ฒด ํํ (console์์ ๋ณด๋ฉด ๋ณต์กํ ๊ตฌ์กฐ)
let jsDate = moment().toDate(); // JavaScript Date ๊ฐ์ฒด๋ก ๋ณํ
console.log(jsDate); // ๐ JavaScript Date ๊ฐ์ฒด (console์์ ๋ ์ง๋ง ์ถ๋ ฅ๋จ)
๐ ์ถ๋ ฅ ๊ฒฐ๊ณผ
Moment<2024-04-01T12:34:56.789Z> // Moment ๊ฐ์ฒด
Mon Apr 01 2024 21:34:56 GMT+0900 (KST) // JavaScript Date ๊ฐ์ฒด
๐ฏ 2๏ธโฃ toDate() ์์ด datepicker์ ์ค์ ํ๋ฉด?
๋ง์ฝ datepicker('setDate', moment())๋ฅผ ์ฌ์ฉํ๋ฉด ์ค๋ฅ๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค.
โ ์๋ชป๋ ์์ (์ค๋ฅ ๋ฐ์ ๊ฐ๋ฅ)
$el.find('[name=rsDt]').datepicker('setDate', moment());
๐ ์ด์ :
- moment()๋ Moment ๊ฐ์ฒด์ด๋ฏ๋ก datepicker๊ฐ ์ฒ๋ฆฌํ ์ ์์.
โ ์ฌ๋ฐ๋ฅธ ์์ (Moment ๊ฐ์ฒด๋ฅผ Date๋ก ๋ณํ)
$el.find('[name=rsDt]').datepicker('setDate', moment().toDate());
728x90
๋ฐ์ํ