728x90
★ DATE
- 시간을 다루는 내장 객체
- 현재 시각
var now = new Date(); //new 생성자함수()
console.log(now);
console.log(now.toString); //javascript는 자동으로 toString을 해줌
- 요소 추출
console.log(now.getYear()); //123
console.log(now.getFullYear()); //2023
console.log(now.getMonth()); //3(0부터)
console.log(now.getDate()); //21
console.log(now.getDay()); //5(일(0)~토(6))
console.log(now.getHours()); //10
console.log(now.getMinutes()); //20
console.log(now.getSeconds()); //20
console.log(now.getMilliseconds()); //135
console.log(now.getTime()); //1682040020135
- 출력 > %tF, %tT, %tA
console.log(now.toLocaleString()); //2023. 4. 21. 오전 10:27:19
console.log(now.toLocaleDateString()); //2023. 4. 21.
console.log(now.toLocaleTimeString()); // 오전 10:27:19
- 특정 시각 만들기
var Christmas = new Date();
Christmas.setFullYear(2023);
Christmas.setMonth(11);
Christmas.setDate(25);
console.log(Christmas.toLocaleDateString()); //2023. 12. 25.
- 연산
- 시각 - 시각
console.log(christmas - now); //21427200001ms
console.log((christmas - now) / 1000 / 60 / 60 / 24); //248
- 시각 + 시간, 시각 - 시간
now.setTime(now.getTime() + (100*24*60*60*1000));
now.setTime(now.getTime() - (100*24*60*60*1000));
console.log(now.toLocaleDateString()); //2023. 7. 30.
728x90
'웹페이지' 카테고리의 다른 글
JAVASCRIPT STEP 9 - BOM (0) | 2023.04.21 |
---|---|
JAVASCRIPT STEP 8 - ARRAY (0) | 2023.04.21 |
JAVASCRIPT STEP 6 - STRING (0) | 2023.04.21 |
JAVASCRIPT STEP 5 - Casting (0) | 2023.04.21 |
JAVASCRIPT STEP 4 - Scope (0) | 2023.04.21 |