Programing Language/Javascript
[Javascript] Date 객체 사용하기
jheaon
2023. 3. 22. 18:59
📚 Javascript에서 Date 객체
Javascript는 Date라는 내장 객체를 가지고 있다. 이는 날짜와 시간을 저장하고 그걸 처리하는 메소드를 포함하고 있다.
📚 Date 사용
일반적인 Date 객체의 생성은 다음과 같이 한다.
const date = new Date();
// result : "2023-03-22T09:48:29.222Z"
📒 Date 객체를 이용한 년,월,일 구하기
const now = new Date(); // 현재 날짜 및 시간
const year = now.getFullYear(); // 연도
const month = now.getMonth(); // 월
const date = now.getDate(); // 일
📒 Date 객체를 이용한 시간, 분, 초 구하기
var now = new Date(); // 현재 날짜 및 시간
var hours = now.getHours(); // 시간
var minutes = now.getMinutes(); // 분
var seconds = now.getSeconds(); // 초