Language/JavaScript

[JS] πŸ“š μžλ°”μŠ€ν¬λ¦½νŠΈ Date λ©”μ†Œλ“œ πŸ’― 총정리

인파_ 2021. 9. 25. 23:55

μžλ°”μŠ€ν¬λ¦½νŠΈ-Date

μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œμ˜ λ‚ μ§œ ν‘œν˜„

β€‹μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œλŠ” Date 객체λ₯Ό μ‚¬μš©ν•˜μ—¬ 맀 μˆœκ°„ λ³€ν™”ν•˜λŠ” μ‹œκ°„κ³Ό λ‚ μ§œμ— κ΄€ν•œ 정보λ₯Ό μ†μ‰½κ²Œ 얻을 수 μžˆλ‹€.

Date κ°μ²΄λŠ” 연월일, μ‹œλΆ„μ΄ˆμ˜ 정보와 ν•¨κ»˜ λ°€λ¦¬μ΄ˆ(millisecond)의 정보도 ν•¨κ»˜ μ œκ³΅ν•œλ‹€.

 

μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ λ‚ μ§œμ™€ μ‹œκ°„μ„ λ‚˜νƒ€λ‚΄κΈ° μœ„ν•œ κ°’μ˜ λ²”μœ„λŠ” λ‹€μŒκ³Ό κ°™λ‹€.

 

1. 연도(year) : 1900λ…„(00) ~ 1999λ…„(99)

2. μ›”(month) : 1μ›”(0) ~ 12μ›”(11)

3. 일(day) : 1일(1) ~ 31일(31)

4. μ‹œ(hours) : 0μ‹œ(0) ~ 23μ‹œ(23)

5. λΆ„(minutes) : 0λΆ„(0) ~ 59λΆ„(59)

6. 초(seconds) : 0초(0) ~ 59초(59)

μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ μ›”(month)을 λ‚˜νƒ€λ‚Ό λ•ŒλŠ” 1월이 0으둜 ν‘œν˜„λ˜κ³ , 12월이 11둜 ν‘œν˜„λœλ‹€λŠ” 사싀에 μœ μ˜ν•΄μ•Ό ν•œλ‹€.

UTC(ν˜‘μ • μ„Έκ³„μ‹œ: Coordinated Universal Time)λŠ” GMT(κ·Έλ¦¬λ‹ˆμΉ˜ ν‰κ· μ‹œ: Greenwich Mean Time)둜 λΆˆλ¦¬κΈ°λ„ ν•˜λŠ”λ° UTC와 GMTλŠ” 초의 μ†Œμˆ«μ  λ‹¨μœ„μ—μ„œλ§Œ 차이가 λ‚˜κΈ° λ•Œλ¬Έμ— μΌμƒμ—μ„œλŠ” ν˜Όμš©λ˜μ–΄ μ‚¬μš©λœλ‹€.
기술적인 ν‘œκΈ°μ—μ„œλŠ” UTCκ°€ μ‚¬μš©λœλ‹€.

​

KST(Korea Standard Time)λŠ” UTC/GMT에 9μ‹œκ°„μ„ λ”ν•œ μ‹œκ°„μ΄λ‹€. 즉, KSTλŠ” UTC/GMT보닀 9μ‹œκ°„μ΄ λΉ λ₯΄λ‹€.
예λ₯Ό λ“€μ–΄, UTC 00:00 AM은 KST 09:00 AM이닀.

​

ν˜„μž¬μ˜ λ‚ μ§œμ™€ μ‹œκ°„μ€ μžλ°”μŠ€ν¬λ¦½νŠΈ μ½”λ“œκ°€ λ™μž‘ν•œ μ‹œμŠ€ν…œμ˜ μ‹œκ³„μ— μ˜ν•΄ κ²°μ •λœλ‹€.
μ‹œμŠ€ν…œ μ‹œκ³„μ˜ μ„€μ •(timezone, μ‹œκ°„)에 따라 μ„œλ‘œ λ‹€λ₯Έ 값을 κ°€μ§ˆ 수 μžˆλ‹€.


Date Constructor

Date κ°μ²΄λŠ” μƒμ„±μž ν•¨μˆ˜μ΄λ‹€.

Date μƒμ„±μž ν•¨μˆ˜λŠ” λ‚ μ§œμ™€ μ‹œκ°„μ„ κ°€μ§€λŠ” μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•œλ‹€.

μƒμ„±λœ μΈμŠ€ν„΄μŠ€λŠ” 기본적으둜 ν˜„μž¬ λ‚ μ§œμ™€ μ‹œκ°„μ„ λ‚˜νƒ€λ‚΄λŠ” 값을 가진닀.

ν˜„μž¬ λ‚ μ§œμ™€ μ‹œκ°„μ΄ μ•„λ‹Œ λ‹€λ₯Έ λ‚ μ§œμ™€ μ‹œκ°„μ„ 닀루고 싢은 경우, Date μƒμ„±μž ν•¨μˆ˜μ— λͺ…μ‹œμ μœΌλ‘œ ν•΄λ‹Ή λ‚ μ§œμ™€ μ‹œκ°„ 정보λ₯Ό 인수둜 μ§€μ •ν•œλ‹€.

​

new Date()

인수λ₯Ό μ „λ‹¬ν•˜μ§€ μ•ŠμœΌλ©΄ ν˜„μž¬ λ‚ μ§œμ™€ μ‹œκ°„μ„ κ°€μ§€λŠ” μΈμŠ€ν„΄μŠ€λ₯Ό λ°˜ν™˜ν•œλ‹€.

const date = new Date();
console.log(date); // Thu May 16 2019 17:16:13 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)

​

new Date(milliseconds)

인수둜 숫자 νƒ€μž…μ˜ λ°€λ¦¬μ΄ˆλ₯Ό μ „λ‹¬ν•˜λ©΄ 1970λ…„ 1μ›” 1일 00:00(UTC)을 기점으둜 인수둜 μ „λ‹¬λœ λ°€λ¦¬μ΄ˆλ§ŒνΌ κ²½κ³Όν•œ λ‚ μ§œμ™€ μ‹œκ°„μ„ κ°€μ§€λŠ” μΈμŠ€ν„΄μŠ€λ₯Ό λ°˜ν™˜ν•œλ‹€.

// KST(Korea Standard Time)λŠ” GMT(κ·Έλ¦¬λ‹ˆμΉ˜ ν‰κ· μ‹œ: Greenwich Mean Time)에 9μ‹œκ°„μ„ λ”ν•œ μ‹œκ°„μ΄λ‹€.
let date = new Date(0); // κΈ°μ μ—μ„œ 0초 κ²½κ³Ό
console.log(date); // Thu Jan 01 1970 09:00:00 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)

// 86400000msλŠ” 1dayλ₯Ό μ˜λ―Έν•œλ‹€.
// 1s = 1,000ms
// 1m = 60s * 1,000ms = 60,000ms
// 1h = 60m * 60,000ms = 3,600,000ms
// 1d = 24h * 3,600,000ms = 86,400,000ms
date = new Date(86400000);
console.log(date); // Fri Jan 02 1970 09:00:00 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)

​

new Date(dateString)

인수둜 λ‚ μ§œμ™€ μ‹œκ°„μ„ λ‚˜νƒ€λ‚΄λŠ” λ¬Έμžμ—΄μ„ μ „λ‹¬ν•˜λ©΄ μ§€μ •λœ λ‚ μ§œμ™€ μ‹œκ°„μ„ κ°€μ§€λŠ” μΈμŠ€ν„΄μŠ€λ₯Ό λ°˜ν™˜ν•œλ‹€.

μ΄λ•Œ 인수둜 μ „λ‹¬ν•œ λ¬Έμžμ—΄μ€ Date.parse λ©”μ†Œλ“œμ— μ˜ν•΄ 해석 κ°€λŠ₯ν•œ ν˜•μ‹μ΄μ–΄μ•Ό ν•œλ‹€.

let date = new Date('May 16, 2019 17:22:10');
console.log(date); // Thu May 16 2019 17:22:10 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)

date = new Date('2019/05/16/17:22:10');
console.log(date); // Thu May 16 2019 17:22:10 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)

​

new Date(year, month[, day, hour, minute, second, millisecond])

인수둜 λ…„, μ›”, 일, μ‹œ, λΆ„, 초, λ°€λ¦¬μ΄ˆλ₯Ό μ˜λ―Έν•˜λŠ” 숫자λ₯Ό μ „λ‹¬ν•˜λ©΄ μ§€μ •λœ λ‚ μ§œμ™€ μ‹œκ°„μ„ κ°€μ§€λŠ” μΈμŠ€ν„΄μŠ€λ₯Ό λ°˜ν™˜ν•œλ‹€.

μ΄λ•Œ λ…„, 월은 λ°˜λ“œμ‹œ μ§€μ •ν•˜μ—¬μ•Ό ν•œλ‹€. μ§€μ •ν•˜μ§€ μ•Šμ€ μ˜΅μ…˜ μ •λ³΄λŠ” 0 λ˜λŠ” 1으둜 μ΄ˆκΈ°ν™”λœλ‹€.

인수 λ‚΄μš©
year 1900λ…„ μ΄ν›„μ˜ λ…„
month 월을 λ‚˜νƒ€λ‚΄λŠ” 0 ~ 11κΉŒμ§€μ˜ μ •μˆ˜ (주의: 0λΆ€ν„° μ‹œμž‘, 0 = 1μ›”)
day 일을 λ‚˜νƒ€λ‚΄λŠ” 1 ~ 31κΉŒμ§€μ˜ μ •μˆ˜
hour μ‹œλ₯Ό λ‚˜νƒ€λ‚΄λŠ” 0 ~ 23κΉŒμ§€μ˜ μ •μˆ˜
minute 뢄을 λ‚˜νƒ€λ‚΄λŠ” 0 ~ 59κΉŒμ§€μ˜ μ •μˆ˜
second 초λ₯Ό λ‚˜νƒ€λ‚΄λŠ” 0 ~ 59κΉŒμ§€μ˜ μ •μˆ˜
millisecond λ°€λ¦¬μ΄ˆλ₯Ό λ‚˜νƒ€λ‚΄λŠ” 0 ~ 999κΉŒμ§€μ˜ μ •μˆ˜

​

λ…„, 월을 μ§€μ •ν•˜μ§€ μ•Šμ€ 경우 1970λ…„ 1μ›” 1일 00:00(UTC)을 κ°€μ§€λŠ” μΈμŠ€ν„΄μŠ€λ₯Ό λ°˜ν™˜ν•œλ‹€.

// λ…„, μ›”, 일, μ‹œ, λΆ„, 초, λ°€λ¦¬μ΄ˆ

// 월을 λ‚˜νƒ€λ‚΄λŠ” 4λŠ” 5월을 μ˜λ―Έν•œλ‹€.
// 2019/5/1/00:00:00:00
let date = new Date(2019, 4);
console.log(date); // Wed May 01 2019 00:00:00 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)

// 월을 λ‚˜νƒ€λ‚΄λŠ” 4λŠ” 5월을 μ˜λ―Έν•œλ‹€.
// 2019/5/16/17:24:30:00
date = new Date(2019, 4, 16, 17, 24, 30, 0);
console.log(date); // Thu May 16 2019 17:24:30 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)

// new Date(dateString) ν˜•μ‹μ΄ 가독성이 훨씬 μ’‹λ‹€.
date = new Date('2019/5/16/17:24:30:10');
console.log(date); // Thu May 16 2019 17:24:30 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)

​

* Date μƒμ„±μž ν•¨μˆ˜λ₯Ό new μ—°μ‚°μžμ—†μ΄ 호좜

Date μƒμ„±μž ν•¨μˆ˜λ₯Ό new μ—°μ‚°μžμ—†μ΄ ν˜ΈμΆœν•˜λ©΄ μΈμŠ€ν„΄μŠ€λ₯Ό λ°˜ν™˜ν•˜μ§€ μ•Šκ³  결과값을 λ¬Έμžμ—΄λ‘œ λ°˜ν™˜ν•œλ‹€.

let date = Date();
console.log(typeof date); // string 
console.log(date); // "Thu May 16 2019 17:33:03 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)"

let date2 = new Date();
console.log(typeof date2); // object
console.log(date2); // Thu May 16 2019 17:33:03 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ) { }

// Object라 λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν• μˆ˜ μžˆλ‹€.
console.log(date2.getDay()); // 6

Date λ©”μ†Œλ“œ

​

Date.now

1970λ…„ 1μ›” 1일 00:00:00(UTC)을 기점으둜 ν˜„μž¬ μ‹œκ°„κΉŒμ§€ κ²½κ³Όν•œ λ°€λ¦¬μ΄ˆλ₯Ό 숫자둜 λ°˜ν™˜ν•œλ‹€.

​

Date.parse

1970λ…„ 1μ›” 1일 00:00:00(UTC)을 기점으둜 인수둜 μ „λ‹¬λœ 지정 μ‹œκ°„(new Date(dateString)의 μΈμˆ˜μ™€ λ™μΌν•œ ν˜•μ‹)κΉŒμ§€μ˜ λ°€λ¦¬μ΄ˆλ₯Ό 숫자둜 λ°˜ν™˜ν•œλ‹€.

let d = Date.parse('Jan 2, 1970 00:00:00 UTC'); // UTC
console.log(d); // 86400000

d = Date.parse('Jan 2, 1970 09:00:00'); // KST
console.log(d); // 86400000

d = Date.parse('1970/01/02/09:00:00'); // KST
console.log(d); // 86400000

 

Date.UTC

1970λ…„ 1μ›” 1일 00:00:00(UTC)을 기점으둜 인수둜 μ „λ‹¬λœ 지정 μ‹œκ°„κΉŒμ§€μ˜ λ°€λ¦¬μ΄ˆλ₯Ό 숫자둜 λ°˜ν™˜ν•œλ‹€.

Date.UTC λ©”μ†Œλ“œλŠ” new Date(year, month[, day, hour, minute, second, millisecond])와 같은 ν˜•μ‹μ˜ 인수λ₯Ό μ‚¬μš©ν•΄μ•Ό ν•œλ‹€.

Date.UTC λ©”μ†Œλ“œμ˜ μΈμˆ˜λŠ” local time(KST)κ°€ μ•„λ‹Œ UTC둜 μΈμ‹λœλ‹€.

let d = Date.UTC(1970, 0, 2);
console.log(d); // 86400000

d = Date.UTC('1970/1/2');
console.log(d); // NaN

 

Date.prototype.getFullYear / Date.prototype.setFullYear

년도λ₯Ό λ‚˜νƒ€λ‚΄λŠ” 4자리 숫자λ₯Ό λ°˜ν™˜/μ„€μ •ν•œλ‹€. / 년도 이외에 μ›”, 일도 μ„€μ • κ°€λŠ₯ν•˜λ‹€.

const today = new Date();

// 년도 지정
today.setFullYear(2000);

console.log(today); // Tue May 16 2000 17:42:40 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)
console.log(today.getFullYear());  // 2000

// 년도 μ›” 일 지정
today.setFullYear(1900, 0, 1);

console.log(today); // Mon Jan 01 1900 17:42:40 GMT+0827 (ν•œκ΅­ ν‘œμ€€μ‹œ)
console.log(today.getFullYear());  // 1900

​

Date.prototype.getMonth / Date.prototype.setMonth

월을 λ‚˜νƒ€λ‚΄λŠ” 0 ~ 11의 μ •μˆ˜λ₯Ό λ°˜ν™˜/μ„€μ •ν•œλ‹€. 1월은 0, 12월은 11이닀. / μ›” 이외에도 일도 μ„€μ • κ°€λŠ₯ν•˜λ‹€.

const today = new Date();

// 월을 지정
today.setMonth(0); // 1μ›”

console.log(today); // Wed Jan 16 2019 17:45:20 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)
console.log(today.getMonth()); // 0

// μ›”/일을 지정
today.setMonth(11, 1); // 12μ›” 1일

console.log(today); // Sun Dec 01 2019 17:45:20 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)
console.log(today.getMonth()); // 11

​

Date.prototype.getDate / Date.prototype.setDate

λ‚ μ§œ(1 ~ 31)λ₯Ό λ‚˜νƒ€λ‚΄λŠ” μ •μˆ˜λ₯Ό λ°˜ν™˜/μ„€μ •ν•œλ‹€.

const today = new Date();

// λ‚ μ§œ 지정
today.setDate(1);

console.log(today); // Wed May 01 2019 17:47:01 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)
console.log(today.getDate());  // 1

​

Date.prototype.getDay

μš”μΌ(0 ~ 6)λ₯Ό λ‚˜νƒ€λ‚΄λŠ” μ •μˆ˜λ₯Ό λ°˜ν™˜ν•œλ‹€. λ°˜ν™˜κ°’μ€ μ•„λž˜μ™€ κ°™λ‹€.

setDayλŠ” μ—†λ‹€. μš”μΌμ„ μ§€μ •ν•˜λŠ”κ±΄ μ˜λ―Έμ—†λ‹€. dateλ₯Ό μ§€μ •ν•˜λ©΄ μžλ™μœΌλ‘œ μš”μΌμ΄ κ²°μ •λ˜κΈ° λ•Œλ¬Έμ΄λ‹€.

μš”μΌ λ°˜ν™˜κ°’
μΌμš”μΌ 0
μ›”μš”μΌ 1
ν™”μš”μΌ 2
μˆ˜μš”μΌ 3
λͺ©μš”일 4
κΈˆμš”μΌ 5
ν† μš”μΌ 6

​

Date.prototype.getHours / Date.prototype.setHours

μ‹œκ°„(0 ~ 23)λ₯Ό λ‚˜νƒ€λ‚΄λŠ” μ •μˆ˜λ₯Ό λ°˜ν™˜/μ„€μ •ν•œλ‹€. / μ‹œκ°„ 이외 λΆ„, 초, λ°€λ¦¬μ΄ˆλ„ μ„€μ •ν•  수 μžˆλ‹€.

const today = new Date();

// μ‹œκ°„ 지정
today.setHours(7);

console.log(today); // Thu May 16 2019 07:49:06 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)
console.log(today.getHours()); // 7

// μ‹œκ°„/λΆ„/초/λ°€λ¦¬μ΄ˆ 지정
today.setHours(0, 0, 0, 0); // 00:00:00:00

console.log(today); // Thu May 16 2019 00:00:00 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)
console.log(today.getHours()); // 0

​

Date.prototype.getMinutes / Date.prototype.setMinutes

λΆ„(0 ~ 59)λ₯Ό λ‚˜νƒ€λ‚΄λŠ” μ •μˆ˜λ₯Ό λ°˜ν™˜/μ„€μ •ν•œλ‹€. / λΆ„ 이외 초, λ°€λ¦¬μ΄ˆλ„ μ„€μ •ν•  수 μžˆλ‹€.

const today = new Date();

// λΆ„ 지정
today.setMinutes(50);

console.log(today);   // Thu May 16 2019 17:50:30 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)
console.log(today.getMinutes()); // 50

// λΆ„/초/λ°€λ¦¬μ΄ˆ 지정
today.setMinutes(5, 10, 999); // HH:05:10:999

console.log(today);   // Thu May 16 2019 17:05:10 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)
console.log(today.getMinutes()); // 5

​

Date.prototype.getSeconds / Date.prototype.setSeconds

초(0 ~ 59)λ₯Ό λ‚˜νƒ€λ‚΄λŠ” μ •μˆ˜λ₯Ό λ°˜ν™˜/μ„€μ •ν•œλ‹€. / 초 이외 λ°€λ¦¬μ΄ˆλ„ μ„€μ •ν•  수 μžˆλ‹€.

const today = new Date();

// 초 지정
today.setSeconds(30);

console.log(today);   // Thu May 16 2019 17:54:30 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)
console.log(today.getSeconds()); // 30

// 초/λ°€λ¦¬μ΄ˆ 지정
today.setSeconds(10, 0); // HH:MM:10:000

console.log(today);   // Thu May 16 2019 17:54:10 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)
console.log(today.getSeconds()); // 10

​

Date.prototype.getMilliseconds / Date.prototype.setMilliseconds

λ°€λ¦¬μ΄ˆ(0 ~ 999)λ₯Ό λ‚˜νƒ€λ‚΄λŠ” μ •μˆ˜λ₯Ό λ°˜ν™˜/μ„€μ •ν•œλ‹€.

const today = new Date();

// λ°€λ¦¬μ΄ˆ 지정
today.setMilliseconds(123);

console.log(today); // Thu May 16 2019 17:55:45 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)
console.log(today.getMilliseconds());    // 123

​

Date.prototype.getTime / Date.prototype.setTime

1970λ…„ 1μ›” 1일 00:00:00(UTC)λ₯Ό 기점으둜 ν˜„μž¬ μ‹œκ°„κΉŒμ§€ 경과된 λ°€λ¦¬μ΄ˆλ₯Ό λ°˜ν™˜/μ„€μ •ν•œλ‹€.

μœ„μ—μ„œ 배운, Date.parse(), Date.UTC의 함좕 버젼이라고 보면 λœλ‹€.

const today = new Date(); // Fri Jan 01 1970 09:00:00 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)

// 1970λ…„ 1μ›” 1일 00:00:00(UTC)λ₯Ό 기점으둜 ν˜„μž¬ μ‹œκ°„κΉŒμ§€ 경과된 λ°€λ¦¬μ΄ˆ 지정
today.setTime(86400000); // 86400000 === 1day

console.log(today); // Fri Jan 02 1970 09:00:00 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)
console.log(today.getTime());  // 86400000

 

Date.prototype.getTimezoneOffset

UTC와 지정 λ‘œμΌ€μΌ(Locale) μ‹œκ°„κ³Όμ˜ 차이λ₯Ό λΆ„λ‹¨μœ„λ‘œ λ°˜ν™˜ν•œλ‹€.

KST(Korea Standard Time)λŠ” UTC에 9μ‹œκ°„μ„ λ”ν•œ μ‹œκ°„μ΄λ‹€. 즉, UTC = KST - 9h이닀.

const today = new Date();
const x = today.getTimezoneOffset() / 60; // λΆ„λ‹¨μœ„κ°’μ„ 60으둜 λ‚˜λˆˆλ‹€

console.log(today); // Thu May 16 2019 17:58:13 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)
console.log(x);     // -9

​

Date.prototype.toDateString

μ‚¬λžŒμ΄ 읽을 수 μžˆλŠ” ν˜•μ‹μ˜ λ¬Έμžμ—΄λ‘œ λ‚ μ§œλ₯Ό λ°˜ν™˜ν•œλ‹€.

const d = new Date('2019/5/16/18:30');

console.log(d.toString());     // Thu May 16 2019 18:30:00 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)
console.log(d.toDateString()); // Thu May 16 2019

​

Date.prototype.toTimeString

μ‚¬λžŒμ΄ 읽을 수 μžˆλŠ” ν˜•μ‹μ˜ λ¬Έμžμ—΄λ‘œ μ‹œκ°„μ„ λ°˜ν™˜ν•œλ‹€.

const d = new Date('2019/5/16/18:30');

console.log(d.toString());     // Thu May 16 2019 18:30:00 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)
console.log(d.toTimeString()); // 18:30:00 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)

πŸ“ ν˜„μž¬ λ‚ μ§œμ™€ μ‹œκ°„μ„ μ΄ˆλ‹¨μœ„λ‘œ 반볡 좜λ ₯ν•˜λŠ” 예제)

(function printNow() {
  const today = new Date();

  const dayNames = ['(μΌμš”μΌ)', '(μ›”μš”μΌ)', '(ν™”μš”μΌ)', '(μˆ˜μš”μΌ)', '(λͺ©μš”일)', '(κΈˆμš”μΌ)', '(ν† μš”μΌ)'];
  // getDay: ν•΄λ‹Ή μš”μΌ(0 ~ 6)λ₯Ό λ‚˜νƒ€λ‚΄λŠ” μ •μˆ˜λ₯Ό λ°˜ν™˜ν•œλ‹€.
  const day = dayNames[today.getDay()];

  const year = today.getFullYear();
  const month = today.getMonth() + 1;
  const date = today.getDate();
  let hour = today.getHours();
  let minute = today.getMinutes();
  let second = today.getSeconds();

  const ampm = hour >= 12 ? 'PM' : 'AM';

  // 12μ‹œκ°„μ œλ‘œ λ³€κ²½
  hour %= 12;
  hour = hour || 12; // 12μ‹œ,24μ‹œλΌμ„œ λ‚˜λˆ„μ–΄ λ–¨μ–΄μ Έμ„œ 0이면 12둜 μž¬μ„€μ • (12μ‹œκ°„μ œ)

  // 10미만인 λΆ„κ³Ό 초λ₯Ό 2자리둜 λ³€κ²½
  minute = minute < 10 ? '0' + minute : minute;
  second = second < 10 ? '0' + second : second;

  const now = `${year}λ…„ ${month}μ›” ${date}일 ${day} ${hour}:${minute}:${second} ${ampm}`;

  console.log(now);
  setTimeout(printNow, 1000); // 1μ΄ˆλ§ˆλ‹€ μž¬κ·€μ²˜λ¦¬
}());

λ‚ μ§œ 계산 방법 정리

​λͺ‡μΌ μ „, λͺ‡μΌ ν›„ λ‚ μ§œ κ³„μ‚°ν•˜λŠ” 방법

var now = new Date();	// ν˜„μž¬ λ‚ μ§œ 및 μ‹œκ°„
console.log("ν˜„μž¬ : ", now);

var yesterday = new Date(now.setDate(now.getDate() - 1));	// μ–΄μ œ
console.log("μ–΄μ œ : ", yesterday);

var tomorrow = new Date(now.setDate(now.getDate() + 1));	// 내일
console.log("내일 : ", tomorrow);

​

λͺ‡λ‹¬ μ „, λͺ‡λ‹¬ ν›„ λ‚ μ§œ κ³„μ‚°ν•˜λŠ” 방법

var now = new Date();	// ν˜„μž¬ λ‚ μ§œ 및 μ‹œκ°„
console.log("ν˜„μž¬ : ", now);

var oneMonthAgo = new Date(now.setMonth(now.getMonth() - 1));	// ν•œλ‹¬ μ „
console.log("ν•œλ‹¬ μ „ : ", oneMonthAgo);

var oneMonthLater = new Date(now.setMonth(now.getMonth() + 1));	// ν•œλ‹¬ ν›„
console.log("ν•œλ‹¬ ν›„ : ", oneMonthLater);

​

λͺ‡λ…„ μ „, λͺ‡λ…„ ν›„ λ‚ μ§œ κ³„μ‚°ν•˜λŠ” 방법

var now = new Date();	// ν˜„μž¬ λ‚ μ§œ 및 μ‹œκ°„
console.log("ν˜„μž¬ : ", now);

var oneMonthAgo = new Date(now.setMonth(now.getMonth() - 1));	// ν•œλ‹¬ μ „
console.log("ν•œλ‹¬ μ „ : ", oneMonthAgo);

var oneMonthLater = new Date(now.setMonth(now.getMonth() + 1));	// ν•œλ‹¬ ν›„
console.log("ν•œλ‹¬ ν›„ : ", oneMonthLater);

μžλ°”μŠ€ν¬λ¦½νŠΈ Date.prototype getter λ©”μ†Œλ“œ

λ©”μ†Œλ“œ μ„€λͺ… κ°’μ˜ λ²”μœ„
getDate() ν˜„μ§€ μ‹œκ°μœΌλ‘œ ν˜„μž¬ μΌμžμ— ν•΄λ‹Ήν•˜λŠ” 숫자λ₯Ό λ°˜ν™˜ν•¨. 1 ~ 31
getDay() ν˜„μ§€ μ‹œκ°μœΌλ‘œ ν˜„μž¬ μš”μΌμ— ν•΄λ‹Ήν•˜λŠ” 숫자λ₯Ό λ°˜ν™˜ν•¨. 0 ~ 6
getMonth() ν˜„μ§€ μ‹œκ°μœΌλ‘œ ν˜„μž¬ 월에 ν•΄λ‹Ήν•˜λŠ” 숫자λ₯Ό λ°˜ν™˜ν•¨. 0 ~ 11
getFullYear() ν˜„μ§€ μ‹œκ°μœΌλ‘œ ν˜„μž¬ 연도λ₯Ό 4λΉ„νŠΈμ˜ 숫자(YYYY)둜 λ°˜ν™˜ν•¨. YYYY
getHours() ν˜„μ§€ μ‹œκ°μœΌλ‘œ ν˜„μž¬ μ‹œκ°μ— ν•΄λ‹Ήν•˜λŠ” 숫자λ₯Ό λ°˜ν™˜ν•¨. 0 ~ 23
getMilliseconds() ν˜„μ§€ μ‹œκ°μœΌλ‘œ ν˜„μž¬ μ‹œκ°μ˜ λ°€λ¦¬μ΄ˆμ— ν•΄λ‹Ήν•˜λŠ” 숫자λ₯Ό λ°˜ν™˜ν•¨. 0 ~ 999
getMinutes() ν˜„μ§€ μ‹œκ°μœΌλ‘œ ν˜„μž¬ μ‹œκ°μ˜ 뢄에 ν•΄λ‹Ήν•˜λŠ” 숫자λ₯Ό λ°˜ν™˜ν•¨. 0 ~ 59
getSeconds() ν˜„μ§€ μ‹œκ°μœΌλ‘œ ν˜„μž¬ μ‹œκ°μ˜ μ΄ˆμ— ν•΄λ‹Ήν•˜λŠ” 숫자λ₯Ό λ°˜ν™˜ν•¨. 0 ~ 59
getTime() 1970λ…„ 1μ›” 1일 0μ‹œ 0λΆ„ 0μ΄ˆλΆ€ν„° ν˜„μž¬κΉŒμ§€μ˜ μ‹œκ°„μ„ λ°€λ¦¬μ΄ˆ λ‹¨μœ„λ‘œ ν™˜μ‚°ν•œ 값을 숫자둜 λ°˜ν™˜ν•¨. -
getTimezoneOffset() UTCλ‘œλΆ€ν„° ν˜„μž¬ μ‹œκ°κΉŒμ§€μ˜ μ‹œκ°„μ°¨λ₯Ό λΆ„ λ‹¨μœ„λ‘œ ν™˜μ‚°ν•œ 값을 숫자둜 λ°˜ν™˜ν•¨. -

​

μžλ°”μŠ€ν¬λ¦½νŠΈ Date.prototype UTC getter λ©”μ†Œλ“œ

λ©”μ†Œλ“œ μ„€λͺ…
getUTCDate() ν˜‘μ •μ„Έκ³„μ‹œ(UTC)둜 ν˜„μž¬ μΌμžμ— ν•΄λ‹Ήν•˜λŠ” 숫자λ₯Ό λ°˜ν™˜ν•¨.
getUTCDay() ν˜‘μ •μ„Έκ³„μ‹œ(UTC)둜 ν˜„μž¬ μš”μΌμ— ν•΄λ‹Ήν•˜λŠ” 숫자λ₯Ό λ°˜ν™˜ν•¨.
getUTCMonth() ν˜‘μ •μ„Έκ³„μ‹œ(UTC)둜 ν˜„μž¬ 월에 ν•΄λ‹Ήν•˜λŠ” 숫자λ₯Ό λ°˜ν™˜ν•¨.
getUTCFullYear() ν˜‘μ •μ„Έκ³„μ‹œ(UTC)둜 ν˜„μž¬ 연도λ₯Ό 4λΉ„νŠΈμ˜ 숫자(YYYY)둜 λ°˜ν™˜ν•¨.
getUTCHours() ν˜‘μ •μ„Έκ³„μ‹œ(UTC)둜 ν˜„μž¬ μ‹œκ°μ— ν•΄λ‹Ήν•˜λŠ” 숫자λ₯Ό λ°˜ν™˜ν•¨.
getUTCMilliseconds() ν˜‘μ •μ„Έκ³„μ‹œ(UTC)둜 ν˜„μž¬ μ‹œκ°μ˜ λ°€λ¦¬μ΄ˆμ— ν•΄λ‹Ήν•˜λŠ” 숫자λ₯Ό λ°˜ν™˜ν•¨.
getUTCMinutes() ν˜‘μ •μ„Έκ³„μ‹œ(UTC)둜 ν˜„μž¬ μ‹œκ°μ˜ 뢄에 ν•΄λ‹Ήν•˜λŠ” 숫자λ₯Ό λ°˜ν™˜ν•¨.
getUTCSeconds() ν˜‘μ •μ„Έκ³„μ‹œ(UTC)둜 ν˜„μž¬ μ‹œκ°μ˜ μ΄ˆμ— ν•΄λ‹Ήν•˜λŠ” 숫자λ₯Ό λ°˜ν™˜ν•¨.

​

μžλ°”μŠ€ν¬λ¦½νŠΈ Date.prototype setter λ©”μ†Œλ“œ

λ©”μ†Œλ“œ μ„€λͺ… κ°’μ˜ λ²”μœ„
setDate() ν˜„μ§€ μ‹œκ°μœΌλ‘œ νŠΉμ • 일자λ₯Ό 섀정함. 1 ~ 31
setMonth() ν˜„μ§€ μ‹œκ°μœΌλ‘œ νŠΉμ • 월을 섀정함. 0 ~ 11
setFullYear() ν˜„μ§€ μ‹œκ°μœΌλ‘œ νŠΉμ • 연도λ₯Ό 섀정함. (μ—°λ„λΏλ§Œ μ•„λ‹ˆλΌ μ›”κ³Ό μΌμžλ„ μ„€μ •ν•  수 있음) YYYY, MM, DD
setHours() ν˜„μ§€ μ‹œκ°μœΌλ‘œ νŠΉμ • μ‹œκ°„μ„ 섀정함. 0 ~ 23
setMilliseconds() ν˜„μ§€ μ‹œκ°μœΌλ‘œ νŠΉμ • λ°€λ¦¬μ΄ˆλ₯Ό 섀정함. 0 ~ 999
setMinutes() ν˜„μ§€ μ‹œκ°μœΌλ‘œ νŠΉμ • 뢄을 섀정함. 0 ~ 59
setSeconds() ν˜„μ§€ μ‹œκ°μœΌλ‘œ νŠΉμ • 초λ₯Ό 섀정함. 0 ~ 59
setTime() 1970λ…„ 1μ›” 1일 0μ‹œ 0λΆ„ 0μ΄ˆλΆ€ν„° λ°€λ¦¬μ΄ˆ λ‹¨μœ„λ‘œ ν‘œν˜„λ˜λŠ” νŠΉμ • μ‹œκ°„μ„ 섀정함. -
β€‹μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ setDay() λ©”μ†Œλ“œλŠ” μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

 

μžλ°”μŠ€ν¬λ¦½νŠΈ Date.prototype UTC setter λ©”μ†Œλ“œ

λ©”μ†Œλ“œ μ„€λͺ… κ°’μ˜ λ²”μœ„
setUTCDate() ν˜‘μ •μ„Έκ³„μ‹œ(UTC)둜 νŠΉμ • 일자λ₯Ό 섀정함. 1 ~ 31
setUTCMonth() ν˜‘μ •μ„Έκ³„μ‹œ(UTC)둜 νŠΉμ • 월을 섀정함. 0 ~ 11
setUTCFullYear() ν˜‘μ •μ„Έκ³„μ‹œ(UTC)둜 νŠΉμ • 연도λ₯Ό 섀정함. (μ—°λ„λΏλ§Œ μ•„λ‹ˆλΌ μ›”κ³Ό μΌμžλ„ μ„€μ •ν•  수 있음) YYYY, MM, DD
setUTCHours() ν˜‘μ •μ„Έκ³„μ‹œ(UTC)둜 νŠΉμ • μ‹œκ°„μ„ 섀정함. 0 ~ 23
setUTCMilliseconds() ν˜‘μ •μ„Έκ³„μ‹œ(UTC)둜 νŠΉμ • λ°€λ¦¬μ΄ˆλ₯Ό 섀정함. 0 ~ 999
setUTCMinutes() ν˜‘μ •μ„Έκ³„μ‹œ(UTC)둜 νŠΉμ • 뢄을 섀정함. 0 ~ 59
setUTCSeconds() ν˜‘μ •μ„Έκ³„μ‹œ(UTC)둜 νŠΉμ • 초λ₯Ό 섀정함. 0 ~ 59