[JS] π μλ°μ€ν¬λ¦½νΈ Date λ©μλ π― μ΄μ 리
μλ°μ€ν¬λ¦½νΈμμμ λ μ§ νν βμλ°μ€ν¬λ¦½νΈμμλ Date κ°μ²΄λ₯Ό μ¬μ©νμ¬ λ§€ μκ° λ³ννλ μκ°κ³Ό λ μ§μ κ΄ν μ 보λ₯Ό μμ½κ² μ»μ μ μλ€. Date κ°μ²΄λ μ°μμΌ, μλΆμ΄μ μ 보μ ν¨κ» λ°λ¦¬μ΄
inpa.tistory.com
π new Date() - date μμ±μ ν¨μ
const date = new Date();
console.log(date); // Thu May 16 2019 17:16:13 GMT+0900 (νκ΅ νμ€μ)Copy
β
- μΈμλ₯Ό μ λ¬νμ§ μμΌλ©΄ νμ¬ λ μ§μ μκ°μ κ°μ§ μΈμ€ν΄μ€ λ°ν
- λ³μ dateλ Date νμ
π new Date(dateString)
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]) λ±μ μΈμλ‘ λ£μ΄ μ§μ λ λ μ§μ μκ°μ κ°μ§ μΈμ€ν΄μ€λ₯Ό λ°νν μ μλ€. μ΄λ λ , μμ λ°λμ μ§μ ν΄μΌ νλ€.
β¨ Date μμ±μ ν¨μλ₯Ό new μ°μ°μ μμ΄ νΈμΆλ κ°λ₯
- μΈμ€ν΄μ€λ₯Ό λ°ννμ§ μκ³ κ²°κ³Όκ°μ λ¬Έμμ΄λ‘ λ°ννλ€.
let date = new Date(2019, 4);
date = new Date(2019, 4, 16, 17, 24, 30, 0);
//μ΄κ²λ κ°λ₯
date = new Date('2019/5/16/17:24:30:10');
-λ¬Έμμ΄λ‘ λ°ννλ€κ³ ν΄μ λ©μλλ₯Ό μ¬μ©ν μ μλ κ²μ μλλ€
let date = Date();
console.log(typeof date); // string
console.log(date); // "Thu May 16 2019 17:33:03 GMT+0900 (νκ΅ νμ€μ)"
//objectλΌ λ©μλ μ¬μ©ν μ μμ
console.log(date2.getDay());
πDate λ©μλ
π» Date.now
- 1970λ 1μ 1μΌ κΈ°μ μΌλ‘ νμ¬ μκ°κΉμ§ κ²½κ³Όν λ°λ¦¬μ΄λ₯Ό μ«μλ‘ λ°ν
Date.parse
- 1970λ 1μ 1μΌ κΈ°μ μΌλ‘ μΈμλ‘ μ λ¬λ μ§μ μκ°κΉμ§μ λ°λ¦¬μ΄λ₯Ό μ«μλ‘ λ°ν
let d = Date.parse('Jan 2, 1970 00:00:00 UTC'); // UTC
console.log(d); // 86400000
π» Date.UTC
- 1970λ μ κΈ°μ μΌλ‘ μΈμλ‘ μ λ¬λ μ§μ μκ°κΉμ§μ λ°λ¦¬μ΄λ₯Ό μ«μλ‘ λ°ν
- new Date(year, month ...)μ κ°μ νμμ μΈμλ₯Ό μ¬μ©
π» Date.prototype.getFullYear/setFullYear
-λ λλ₯Ό λνλ΄λ 2μ리 μ«μλ₯Ό λ°ν
const today = new Date();
// λ
λ μ§μ
today.setFullYear(2000);
console.log(today); // Tue May 16 2000 17:42:40 GMT+0900 (νκ΅ νμ€μ)
console.log(today.getFullYear()); // 2000
π» Date.prototype.getFullMonth/setFullMonth
π» Date.prototype.getFullDate/setFullDate
- λκ°μ μ€λͺ (μ, μΌ μΈν νκΈ°/κ°μ Έμ€κΈ°)
π» Date.prototype.getDay
- μμΌμ λνλ΄λ μ μ
- setDayκ° μμ. dateλ₯Ό μ§μ νλ©΄ μλμΌλ‘ μμΌμ΄ κ²°μ λκΈ° λλ¬Έ
-μΌμμΌμ΄ 0, μμμΌμ΄ 1 ~ ν μμΌ6
π» Date.prototype.getHours / setHours
- μκ°(0-23)λ₯Ό λνλ΄λ μ μ
π» Date.prototype.getMinutes / setMinutes
- λΆ(0-59)λ₯Ό λνλ΄λ μ μ
π» Date.prototype.getSeconds / setSeconds
μ΄(0-59)λ₯Ό λνλ΄λ μ μ
π» Date.prototype.getMillliseconds / setMilliseconds
-λ°λ¦¬μ΄(0-999)λ₯Ό λνλ΄λ μ μ
π» Date.prototype.toString
onst event = new Date('August 19, 1975 23:15:30');
console.log(event.toString());
// Expected output: "Tue Aug 19 1975 23:15:30 GMT+0200 (CEST)"
- date κ°μ²΄μ μκ°μ λ¨μ λ¬Έμμ΄λ‘ λ³ν
π» 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 (νκ΅ νμ€μ)
π» Date.prototype.toISOString() β
- λ μ§μ ν¬λ§·μ λ§μΆ°μ£Όλ ν¨μ
- YYYYYY-MM-DDTHH:mm:ss.sssZ μ΄λ° λ³νκ°
const a = new Date()
a.toISOString()
//2024-09-07T15:23:49.548Z
//λ³νκ°μ΄ νκ΅μκ°μμ 9μκ° λΉ μ§ κ° μΆλ ₯
μ΄ λ μ΄μ©νλ κ²μ΄
β¨ Date.getTimezoneOffset()
- offset μ체 λ»μ "νΈμ°¨"
- νμ§μ κΈ°μ€ μκ°μ μ°¨μ΄λ₯Ό κ°μ Έμ΄
const offset = new Date().getTimezoneOffset() * 60000;
const today = new Date(Date.now() - offset);
'μ 리 λͺ¨μ β¨' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[μλ°μ€ν¬λ¦½νΈ] math λ©μλ μ 리 β¨ (1) | 2024.09.20 |
---|---|
[μλ°μ€ν¬λ¦½νΈ] λ°°μ΄ λ©μλ μ 리 β¨ (1) | 2024.01.16 |