2.1 Date ๊ฐ์ฒด
๋ ์ง์ ์ซ์๋ฅผ ๋ค๋ฃจ๋ ๊ฒ์ ์น ์ ํ๋ฆฌ์ผ์ด์
๊ฐ๋ฐ์์ ์ค์ํ ๋ถ๋ถ์ด์ผ. JavaScript๋ ๋ ์ง์ ์ซ์๋ฅผ ์กฐ์ํ ์ ์๋ ๋ด์ฅ ๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ ์ ๊ณตํด. ์ด ๊ฐ์์์๋ Date
๊ฐ์ฒด, ๊ทธ ๋ฉ์๋, ๊ทธ๋ฆฌ๊ณ ๋ ์ง ๋ฐ ์ซ์ ํฌ๋งทํ
๋ฐฉ๋ฒ์ ์ดํด๋ณผ ๊ฑฐ์ผ.
JavaScript์ Date
๊ฐ์ฒด๋ ๋ ์ง์ ์๊ฐ์ ๋ํ๋ด. ํ์ฌ ๋ ์ง์ ์๊ฐ์ ๊ฐ์ ธ์ค๊ฑฐ๋ ํน์ ๋ ์ง์ ์๊ฐ์ ์ค์ ํ๊ณ , ์ด๋ฅผ ํฌ๋งทํ
ํ ๋ค์ํ ๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์์ด.
Date ๊ฐ์ฒด ์์ฑํ๊ธฐ
Date()
๊ฐ์ฒด๋ฅผ ์์ฑํ๋ฉด, ๊ทธ ์๊ฐ์
ํ์ฌ ๋ ์ง์ ์๊ฐ์ ๊ฐ์ง ๊ฑฐ์ผ.
Date
๊ฐ์ฒด๋ ์ฌ๋ฌ ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ์์ฑํ ์ ์์ด.
1. ํ์ฌ ๋ ์ง์ ์๊ฐ:
const now = new Date();
console.log(now); // ํ์ฌ ๋ ์ง์ ์๊ฐ์ ์ถ๋ ฅ
2. ํน์ ๋ ์ง์ ์๊ฐ ์ค์ :
const specificDate = new Date('2024-07-07T10:00:00');
console.log(specificDate); // 2024๋
7์ 7์ผ ๋ ์ง ์ถ๋ ฅ
3. ์ซ์ ๊ฐ์ผ๋ก ๋ ์ง์ ์๊ฐ ์ค์ :
const anotherDate = new Date(2024, 6, 7, 10, 0, 0);
console.log(anotherDate); // 2024๋
7์ 7์ผ ๋ ์ง ์ถ๋ ฅ
2.2 Date() ๊ฐ์ฒด ๋ฉ์๋
๋ ์ง ๊ตฌ์ฑ ์์ ์ป๊ธฐ:
getFullYear()
: ์ฐ๋ ์ป๊ธฐ (๋ค ์๋ฆฌ)getMonth()
: ์ ์ป๊ธฐ (0๋ถํฐ 11๊น์ง)getDate()
: ์์ ์ผ ์ป๊ธฐ (1๋ถํฐ 31๊น์ง)getHours()
: ์๊ฐ ์ป๊ธฐ (0๋ถํฐ 23๊น์ง)getMinutes()
: ๋ถ ์ป๊ธฐ (0๋ถํฐ 59๊น์ง)getSeconds()
: ์ด ์ป๊ธฐ (0๋ถํฐ 59๊น์ง)
์์:
const now = new Date();
console.log(now.getFullYear()); // ํ์ฌ ์ฐ๋
console.log(now.getMonth()); // ํ์ฌ ์ (-1)
console.log(now.getDate()); // ํ์ฌ ์ผ
console.log(now.getHours()); // ํ์ฌ ์๊ฐ
console.log(now.getMinutes()); // ํ์ฌ ๋ถ
console.log(now.getSeconds()); // ํ์ฌ ์ด
๋ ์ง ๊ตฌ์ฑ ์์ ์ค์ :
setFullYear(year)
: ์ฐ๋ ์ค์ setMonth(month)
: ์ ์ค์ setDate(day)
: ์์ ์ผ ์ค์ setHours(hours)
: ์๊ฐ ์ค์ setMinutes(minutes)
: ๋ถ ์ค์ setSeconds(seconds)
: ์ด ์ค์
์์:
const date = new Date();
date.setFullYear(2025);
date.setMonth(0); // 1์
date.setDate(15);
date.setHours(12);
date.setMinutes(30);
date.setSeconds(45);
console.log(date.toString());
2.3 ๋ ์ง ํฌ๋งทํ
JavaScript๋ ๋ฌธ์์ด๋ก ๋ ์ง๋ฅผ ํฌ๋งทํ
ํ ์ ์๋ ๋ฉ์๋๋ฅผ ์ ๊ณตํด. ๊ฐ์ฅ ์ธ๊ธฐ ์๋ ๋ฐฉ๋ฒ์ toLocaleDateString()
๋ก, ๋ก์บ์ ๋ฐ๋ผ ๋ ์ง๋ฅผ ํฌ๋งทํ
ํด.
toLocaleDateString() ์ฌ์ฉ ์์:
const now = new Date();
console.log(now.toLocaleDateString('en-US')); // "7/7/2024" (MM/DD/YYYY)
console.log(now.toLocaleDateString('en-GB')); // "07/07/2024" (DD/MM/YYYY)
console.log(now.toLocaleDateString('ru-RU')); // "07.07.2024" (DD.MM.YYYY)
๋ฉ์๋ toLocaleTimeString()
toLocaleTimeString()
๋ฉ์๋๋ ์ง์ญํ๋ ํฌ๋งท์ผ๋ก ์๊ฐ ๋ฌธ์์ด์ ๋ฐํํด:
const now = new Date();
console.log(now.toLocaleTimeString('en-CA'));
console.log(now.toLocaleTimeString('en-GB'));
๋ฉ์๋ toLocaleString()
toLocaleString()
๋ฉ์๋๋ ๋ ์ง์ ์๊ฐ์ ์ง์ญํ๋ ํฌ๋งท์ผ๋ก ๋ฌธ์์ด๋ก ๋ฐํํด:
const now = new Date();
console.log(now.toLocaleString('en-CA'));
console.log(now.toLocaleString('en-GB'));
๋ฉ์๋ toISOString()
toISOString()
๋ฉ์๋๋ ISO 8601 ํฌ๋งท์ผ๋ก ๋ ์ง์ ์๊ฐ์ ๋ฐํํด:
const now = new Date();
console.log(now.toISOString()); // "2023-07-05T10:00:00.000Z"
2.4 ์ซ์ ํฌ๋งทํ
๋ฉ์๋ toLocaleString()
toLocaleString()
๋ฉ์๋๋ ๋ก์ปฌ ์ค์ ์ ๋ฐ๋ผ ์ซ์๋ฅผ ํฌ๋งทํ
ํ ์ ์์ด:
const number = 1234567.89;
console.log(number.toLocaleString()); // ๋ฏธ๊ตญ ํฌ๋งท "1,234,567.89" ๋๋ ๋ก์ปฌ ํฌ๋งท "1 234 567,89"
console.log(number.toLocaleString('de-DE')); // ๋
์ผ ํฌ๋งท "1.234.567,89"
Intl.NumberFormat๋ฅผ ์ฌ์ฉํ ์ซ์ ํฌ๋งทํ
Intl.NumberFormat
์์ฑ์๋ ์ซ์๋ฅผ ํฌ๋งทํ
ํ๋ ๋ ์ ์ฐํ ์ต์
์ ์ ๊ณตํด:
const number = 1234567.89;
const usFormatter = new Intl.NumberFormat('en-US');
console.log(usFormatter.format(number)); // "1,234,567.89"
const deFormatter = new Intl.NumberFormat('de-DE');
console.log(deFormatter.format(number)); // "1.234.567,89"
const customFormatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
});
console.log(customFormatter.format(number)); // "$1,234,567.89"
GO TO FULL VERSION