30μ₯ Date
νμ€ λΉνΈμΈ Dateλ λ μ§μ μκ° κ΄λ ¨ λ©μλλ₯Ό μ 곡νλ λΉνΈμΈ κ°μ²΄ μ΄λ©΄μ μμ±μ ν¨μ λ€.
30.1 Date μμ±μ ν¨μ
Date λ μμ±μ ν¨μλ€. Date μμ±μ ν¨μλ‘ μμ±ν Date κ°μ²΄λ κΈ°λ³Έμ μΌλ‘ νμ¬ λ μ§μ μκ°μ λνλ΄λ μ μκ°μ κ°μ§λ€. νμ¬κ° μλ νΉμ μκ°μ λ€λ£¨κ³ μΆλ€λ©΄ λͺ μμ μΌλ‘ ν΄λΉ μ 보λ₯Ό μΈμλ‘ μ§μ νλ€.
30.1.1 new Date()
μΈμ μμ΄ new μ°μ°μμ νΈμΆνλ©΄ νμ¬ λ μ§μ μκ°μ κ°λ Date κ°μ²΄λ₯Ό λ°ννλ€. λ΄λΆμ μΌλ‘ λ μ§μ μκ°μ λνλ΄λ μ μκ°μ κ°μ§λ§ μ½μμ μΆλ ₯νλ©΄ λ μ§μ μκ° μ 보λ₯Ό μΆλ ₯νλ€.
new Date() // Thu Feb 09 2023 11:13:23 GMT+0900 (νκ΅ νμ€μ) {}30.1.2 new Date(milliseconds)
μ«μ νμ μ λ°λ¦¬μ΄λ₯Ό μ λ¬νλ©΄ 1970λ 1μ 1μΌ 0μ 0λΆ 0μ΄λ₯Ό κΈ°μ μΌλ‘ μ λ¬ν λ°λ¦¬μ΄λ§νΌ κ²½κ³Όν λ μ§μ μκ°μ λνλ΄λ Date κ°μ²΄λ₯Ό λ°ννλ€.
new Date(0) // Thu Jan 01 1970 09:00:00 GMT+0900 (νκ΅ νμ€μ) {}30.1.3 new Date(dateString)
λ μ§μ μκ°μ λνλ΄λ λ¬Έμμ΄μ μΈμλ‘ μ λ¬νλ©΄ μ§μ λ λ μ§μ μκ°μ λνλ΄λ Date κ°μ²΄λ₯Ό λ°ννλ€. Date.parse λ©μλμ μν΄ ν΄μ κ°λ₯ν νμμ΄μ΄μΌ νλ€.
new Date('May 26, 2020 10:00:00')
// Thu May 26 2020 10:00:00 GMT+0900 (νκ΅ νμ€μ) {}30.1.4 new Date(year, month[, day, hour, minute, second, millisecond])
μ°, μ, μΌ, μ, λΆ, μ΄, λ°λ¦¬μ΄λ₯Ό μλ―Ένλ μ«μλ₯Ό μ λ¬νλ©΄ μ§μ λ λ μ§μ μκ°μ λνλ΄λ Date κ°μ²΄λ₯Ό λ°ννλ€. μ°, μμ νμκ°μ΄λ€.
new Date(2020, 2) // => μμ 2λ 3μμ μλ―Ένλ€.
// Sun Mar 01 2020 00:00:00 GMT+090030.2 Date λ©μλ
30.2.1 Date.now
1970λ 1μ 1μΌ κΈ°μ μΌλ‘ νμ¬ μκ°κΉμ§ κ²½κ³Όν λ°λ¦¬μ΄λ₯Ό μ«μλ‘ λ°ννλ€.
const now = Date.now() // 167590955730730.2.2 Date.parse
1970λ 1μ 1μΌ κΈ°μ μΌλ‘ μ λ¬λ μ§μ μκ°κΉμ§μ λ°λ¦¬μ΄λ₯Ό μ«μλ‘ λ°ννλ€.
Date.parse('Jan 2, 1970 00:00:00 UTC') // 86400000
Date.parse('Jan 2, 1970 00:00:00') // 86400000
Date.parse('1970/01/02/09:00:00') // 8640000030.2.3 Date.UTC
1970λ 1μ 1μΌ κΈ°μ μΌλ‘ μ λ¬λ μ§μ μκ°κΉμ§μ λ°λ¦¬μ΄λ₯Ό μ«μλ‘ λ°ννλ€.
Date.UTC(1970, 0, 2) // 8640000030.2.4 Date.prototype.getFullYear
Date κ°μ²΄μ μ°λλ₯Ό λνλ΄λ μ μλ₯Ό λ°ννλ€.
new Date('2020/07/24').getFullYear() // 202030.2.5 Date.prototype.setFullYear
Date κ°μ²΄μ μ°λλ₯Ό λνλ΄λ μ μλ₯Ό μ€μ νλ€. μ, μΌλ μ§μ κ°λ₯
const today = new Date()
today.setFullYear(2000)
today.getFullYear() // 2000
today.setFullYear(2000, 2, 1)30.2.6 Date.prototype.getMonth
Date κ°μ²΄μ μμ λνλ΄λ μ μλ₯Ό λ°ννλ€. 0μ΄ 1μμ΄λ€.
30.2.7 Date.prototype.setMonth
Date κ°μ²΄μ μμ λνλ΄λ μ μλ₯Ό μ€μ νλ€. μΌλ μ§μ κ°λ₯
30.2.8 Date.prototype.getDate
Date κ°μ²΄μ μΌμ λνλ΄λ μ μλ₯Ό λ°ννλ€.
30.2.9 Date.prototype.setDate
Date κ°μ²΄μ μΌμ λνλ΄λ μ μλ₯Ό μ€μ νλ€.
30.2.10 Date.prototype.getDay
Date κ°μ²΄μ μμΌμ λνλ΄λ μ μλ₯Ό λ°ννλ€. 0 - μΌμμΌ 6 - μμμΌ
30.2.11 Date.prototype.getHours
Date κ°μ²΄μ μκ°μ λνλ΄λ μ μλ₯Ό λ°ννλ€.
30.2.12 Date.prototype.setHours
Date κ°μ²΄μ μκ°μ λνλ΄λ μ μλ₯Ό μ€μ νλ€. λΆ, μ΄, λ°λ¦¬μ΄λ μ€μ κ°λ₯
30.2.13 Date.prototype.getMinutes
Date κ°μ²΄μ λΆμ λνλ΄λ μ μλ₯Ό λ°ννλ€.
30.2.14 Date.prototype.setMinutes
Date κ°μ²΄μ λΆμ λνλ΄λ μ μλ₯Ό μ€μ νλ€. μ΄, λ°λ¦¬μ΄λ μ€μ κ°λ₯
30.2.15 Date.prototype.getSeconds
Date κ°μ²΄μ μ΄μ λνλ΄λ μ μλ₯Ό λ°ννλ€.
30.2.16 Date.prototype.setSeconds
Date κ°μ²΄μ μ΄λ₯Ό λνλ΄λ μ μλ₯Ό μ€μ νλ€. λ°λ¦¬μ΄λ μ€μ κ°λ₯
30.2.17 Date.prototype.getMilliSeconds
Date κ°μ²΄μ λ°λ¦¬μ΄λ₯Ό λνλ΄λ μ μλ₯Ό λ°ννλ€.
30.2.18 Date.prototype.setMilliSeconds
Date κ°μ²΄μ λ°λ¦¬μ΄λ₯Ό λνλ΄λ μ μλ₯Ό μ€μ νλ€.
30.2.19 Date.prototype.getTime
1970λ 1μ 1μΌμ κΈ°μ μΌλ‘ Date κ°μ²΄μ μκ°κΉμ§ κ²½κ³Όλ λ°λ¦¬μ΄λ₯Ό λ°ννλ€.
30.2.20 Date.prototype.setTime
Date κ°μ²΄μ 1970λ 1μ 1μΌμ κΈ°μ μΌλ‘ κ²½κ³Όλ λ°λ¦¬μ΄λ₯Ό μ€μ νλ€.
30.2.21 Date.prototype.getTimezoneOffset
UTCμ Date κ°μ²΄μ μ§μ λ λ‘μΊ μκ°κ³Όμ μ°¨μ΄λ₯Ό λΆ λ¨μλ‘ λ°ννλ€.
30.2.22 Date.prototype.toDateString
λ¬Έμμ΄λ‘ Date κ°μ²΄μ λ μ§λ₯Ό λ°ννλ€.
30.2.23 Date.prototype.toTimeString
Date κ°μ²΄μ μκ°μ ννν λ¬Έμμ΄μ λ°ννλ€.
30.2.24 Date.prototype.toISOString
ISO 8601 νμμΌλ‘ Date κ°μ²΄μ λ μ§μ μκ°μ ννν λ¬Έμμ΄μ λ°ννλ€.
30.2.25 Date.prototype.toLocaleString
μ λ¬ν λ‘μΊμ κΈ°μ€μΌλ‘ Date κ°μ²΄μ λ μ§μ μκ°μ ννν λ¬Έμμ΄μ λ°ννλ€.
30.2.26 Date.prototype.toLocaleTimeString
μ λ¬ν λ‘μΊμ κΈ°μ€μΌλ‘ Date κ°μ²΄μ μκ°μ ννν λ¬Έμμ΄μ λ°ννλ€.
30.3 Dateλ₯Ό νμ©ν μκ³ μμ
(function printNow(){
const today = new Date()
const dayNames = ['μΌμμΌ','μμμΌ','νμμΌ','μμμΌ','λͺ©μμΌ','κΈμμΌ','ν μμΌ']
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"
hour %= 12
hour = hour || 12
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)
}())Last updated
Was this helpful?