ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๋ฏธ๋ฆฌ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๋‚ด์žฅ ๊ฐ์ฒด(Standard built-in objects)๋ผ๊ณ  ํ•œ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ชจ๋“  ๊ฒƒ์€ ๋‹ค ๊ฐ์ฒด๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํŠน๋ณ„ํ•œ ๊ธฐ๋Šฅ๋ณ„๋กœ ๋‹ค์–‘ํ•œ ๋‚ด์žฅ๊ฐ์ฒด๋“ค์ด ์กด์žฌํ•œ๋‹ค. ๊ทธ ์ค‘ ๋Œ€ํ‘œ์ ์ธ๊ฒƒ์ด ๋ฐ”๋กœ ๋‚ ์งœ!

 

๐Ÿ’ก Date ๊ฐ์ฒด ์ƒ์„ฑ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋‚ ์งœ๋Š” Date ๊ฐ์ฒด๋กœ ๋‹ค๋ฃฌ๋‹ค. Date ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

//Date
let myDate = new Date();

์ด๋ ‡๊ฒŒ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  ์ฝ˜์†”์— ์ถœ๋ ฅ์„ ํ•ด๋ณด๋ฉด ์ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ๋‚ ์งœ๊ฐ€ ์ถœ๋ ฅ์ด ๋œ๋‹ค. ์™ผ์ชฝ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์š”์ผ-์›”-์ผ-๋…„๋„-์‹œ๊ฐ„-์‹œ๊ฐ„๋Œ€ ๊ฐ€ ์ถœ๋ ฅ์ด ๋œ๋‹ค. ๋งˆ์ง€๋ง‰์— ์‹œ๊ฐ„๋Œ€๋Š” ์ปดํ“จํ„ฐ๋ฅผ ์–ด๋””์„œ ์‚ฌ์šฉํ•˜๋Š”์ง€์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ์ถœ๋ ฅ์ด ๋œ๋‹ค.

๋ฌด์กฐ๊ฑด ์ƒ์„ฑํ•œ ์ˆœ๊ฐ„๋งŒ ๋งŒ๋“ค์–ด์ง€๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. Date ๊ฐ์ฒด ์•ˆ์— ํŠน์ •ํ•œ ๊ฐ’์„ ์ง‘์–ด ๋„ฃ์œผ๋ฉด ํŠน์ • ๋‚ ์งœ์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์†Œ๊ด„ํ˜ธ ์•ˆ์— ์ˆซ์ž ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ฉด UTC ๊ธฐ์ค€ 1970๋…„ 1์›” 1์ผ 00:00:00 ์—์„œ ๋„ฃ์–ด์ค€ ์ˆซ์ž๊ฐ’์˜ ๋ฐ€๋ฆฌ์ดˆ ๋งŒํผ ์ง€๋‚œ ๋‚ ์งœ์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. (๋ฐ€๋ฆฌ์ดˆ๋Š” 1์ดˆ๋ฅผ 1000์œผ๋กœ ๋‚˜๋ˆˆ ๋‹จ์œ„)

//new Date(milliseconds)
let myDate = new Date(1000);

ํ•˜์ง€๋งŒ ์ˆซ์ž๋ฅผ ๋„ฃ๋Š” ๊ฒƒ์€ ๋ฐ€๋ฆฌ์ดˆ ๋‹จ์œ„๊ฐ€ ๋„ˆ๋ฌด ๋ฏธ์„ธํ•˜๊ธฐ๋„ ํ•˜๊ณ  1970๋…„์€ ๋„ˆ๋ฌด ์˜›๋‚ ์ด๊ธฐ ๋•Œ๋ฌธ์— ํŠน์ •ํ•œ ๋‚ ์งœ (์˜ˆ๋ฅผ ๋“ค์–ด ์–ด์ œ, ๋‚ด์ผ, ๋‹ค์Œ๋‹ฌ ๋“ฑ) ๋ฅผ ๊ฐ์ฒด๋กœ ๋งŒ๋“œ๋ ค๋ฉด ๊ณ„์‚ฐํ•˜๊ธฐ๋„ ํž˜๋“ค๊ณ  ๊ณ„์‚ฐํ•˜๋”๋ผ๋„ ํฐ ์ˆซ์ž๊ฐ’์„ ๋„ฃ์–ด์ฃผ์–ด์•ผํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์ˆซ์ž๋ฅผ ๋„ฃ๋Š” ๊ฒƒ๋ณด๋‹ค ๋ฌธ์ž์—ด์„ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ์ด ๋” ํŽธํ•˜๋‹ค! ๋‚ ์งœ๋กœ ํ•ด์„์ด ๊ฐ€๋Šฅํ•œ ๋ฌธ์ž์—ด์„ ๋„ฃ์–ด์ฃผ๋ฉด ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์ˆ˜ ์žˆ๋‹ค.

// new Date('YYYY-MM-DD')
let myDate = new Date('2017-05-18');

// new Date('YYYY-MM-DDThh:mm:ss')
let myDate = new Date('2017-05-18T19:11:16');

๋งˆ์ง€๋ง‰์œผ๋กœ ์†Œ๊ด„ํ˜ธ ์•ˆ์— ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๊ฐ’์„ ์ „๋‹ฌํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ๋…„๋„-์›”-์ผ-์‹œ-๋ถ„-์ดˆ-๋ฐ€๋ฆฌ์ดˆ ์ˆœ์œผ๋กœ ์ž…๋ ฅํ•˜๋ฉด ๋˜๊ณ , ๋…„๋„์™€ ์›”์€ ํ•„์ˆ˜๋กœ ์ž‘์„ฑํ•ด์•ผํ•œ๋‹ค.

//new Date(year, month, date, hours, minutes, seconds, ms)
let myDate1 = new Date(2017, 4, 18, 19, 11, 16);
let myDate2 = new Date(2017, 4, 18, 19, 11);
let myDate3 = new Date(2017, 4, 18, 19);
let myDate4 = new Date(2017, 4, 18);
let myDate5 = new Date(2017, 4);

month๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘์ด๋ผ์„œ 4๋ฅผ ์ž…๋ ฅํ•˜๋ฉด 5์›”๋กœ ๋‚˜์˜จ๋‹ค.

 

๐Ÿ’ก Date ๊ฐ์ฒด ๋ฉ”์†Œ๋“œ

ํƒ€์ž„์Šคํƒฌํ”„

// Date.getTime()
let myDate = new Date(2017, 4, 18, 19, 11, 16);

console.log(myDate.getTime());

๊ฐ์ฒด๊ฐ€ 1970๋…„ 1์›” 1์ผ 00:00:00 UTC ๋ถ€ํ„ฐ ๋ช‡ ๋ฐ€๋ฆฌ์ดˆ๊ฐ€ ์ง€๋‚ฌ๋Š”์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด ์ •์ˆ˜๊ฐ’์„ ํƒ€์ž…์Šคํƒฌํ”„(time stamp)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์ด ํƒ€์ž„ ์Šคํƒฌํ”„๋Š” ํŠน์ • ๋‚ ์งœ์™€ ์ง€๊ธˆ๋‚ ์งœ ์‚ฌ์ด์— ์–ผ๋งˆ๋‚˜ ์ง€๋‚ฌ๋Š”์ง€ ์•Œ์•„๋ณผ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

// Date.getTime()
let myDate = new Date(2017, 4, 18, 19, 11, 16);
let today = new Date();

let timeDiff = myDate.getTime() - today.getTime();

console.log(timeDiff + '๋ฐ€๋ฆฌ์ดˆ');
console.log(timeDiff + '์ดˆ');
console.log(timeDiff / 1000 / 60 + '๋ถ„');
console.log(timeDiff / 1000 / 60 / 60 + '์‹œ๊ฐ„');

๊ทธ ๋ฐ–์—๋„ Date ๊ฐ์ฒด์˜ ๋‹ค์–‘ํ•œ ๋ฉ”์†Œ๋“œ

let myDate = new Date(2017, 4, 18, 19, 11, 16);

console.log(myDate.getFullYear());		// ๋…„๋„
console.log(myDate.getMonth());			// ์›”
console.log(myDate.getDate());			// ์ผ
console.log(myDate.getDay());			// ์›”(0) ~ ์ผ(6)
console.log(myDate.getHours());			// ์‹œ
console.log(myDate.getMinutes());		// ๋ถ„
console.log(myDate.getSeconds());		// ์ดˆ
console.log(myDate.getMilliseconds());		// ๋ฐ€๋ฆฌ์ดˆ

๊ฐ„๋‹จํ•œ ์‹œ๊ฐ„ ์ •๋ณด

๊ฐ„๋‹จํ•˜๊ฒŒ ์‹œ๊ฐ„ ์ •๋ณด๋ฅผ ์•Œ์•„๋‚ด๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋“ค์€ ์‚ฌ์šฉ์ž์˜ ๋ธŒ๋ผ์šฐ์ €์— ์„ค์ •๋œ ๊ตญ๊ฐ€์˜ ํ‘œ๊ธฐ์— ๋งž์ถฐ ๋‚ ์งœ์™€ ์‹œ๊ฐ„์„ ๋ณด์—ฌ์ค€๋‹ค.

let myDate = new Date();

console.log(myDate.toLocalDateString()); // myDate๊ฐ€ ๊ฐ€์ง„ ๋‚ ์งœ์— ๋Œ€ํ•œ ์ •๋ณด (๋…„. ์›”. ์ผ)
console.log(myDate.toLocaleTimeString()); // myDate๊ฐ€ ๊ฐ€์ง„ ์‹œ๊ฐ„์— ๋Œ€ํ•œ ์ •๋ณด (์‹œ:๋ถ„:์ดˆ)
console.log(myDate.toLocaleString()); 	// myDate๊ฐ€ ๊ฐ€์ง„ ๋‚ ์งœ์™€ ์‹œ๊ฐ„์— ๋Œ€ํ•œ ์ •๋ณด (๋…„. ์›”. ์ผ ์‹œ:๋ถ„:์ดˆ)

๋‚ ์งœ ์ž๋™ ์ˆ˜์ •

๋˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๊ฐ’์„ ์„ค์ •ํ•˜๋ฉด ์ž๋™์œผ๋กœ ๋‚ ์งœ๋ฅผ ์ˆ˜์ •ํ•ด์ค€๋‹ค.

let myDate = new Date(1988, 0, 32); // 1988๋…„ 1์›” 32์ผ์€ ์—†์Šต๋‹ˆ๋‹ค

// 2์›” 1์ผ๋กœ ์ž๋™๊ณ ์นจ ๋˜๋Š”๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
console.log(myDate) // Mon Feb 01 1988 00:00:00

ํ˜„์žฌ ์‹œ๊ฐ„

ํŠน์ •ํ•œ ์‹œ์ ์ด ์•„๋‹ˆ๋ผ ๋‹จ์ˆœํžˆ ์ˆœ๊ฐ„์ˆœ๊ฐ„ ๊ทธ ๋•Œ ๋‹น์‹œ ์‹œ๊ฐ„ ๊ฐ’์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•œ๋‹ค. getTime()์„ ํ˜ธ์ถœํ•œ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜์ง€๋งŒ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์„œ ๋ฉ”๋ชจ๋ฆฌ์˜ ๋ถ€๋‹ด์„ ์ค„์—ฌ์ค€๋‹ค. 

let myDate = new Date();

console.log(Date.now() === myDate.getTime()); // true

 

๐Ÿ’ก Date ๊ฐ์ฒด ์ •๋ณด ์ˆ˜์ •ํ•˜๊ธฐ

set์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜๋ฉด, ์ƒ์„ฑ๋œ Date๊ฐ์ฒด์˜ ์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋Œ€๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์ง„ ์š”์†Œ๋“ค์€ ์„ ํƒ์ ์ธ ์š”์†Œ์ด๋‹ค.

 

 

์ฐธ๊ณ  - developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Date

๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€