웹 프로그래밍 기초/객체
get메서드 / set메서드
별초롱언니
2025. 4. 9. 17:12
get 메서드
날짜의 일부를 가져오는데 사용한다.
get 메서드 | 설명 |
getDate() | (1~31)의 숫자로 날짜를 구한다 |
getDay() | (0~6)의 숫자로 요일을 구한다. 0은 일요일 |
getFullYear() | 4자릿수의 년도를 구한다 |
getHours() | (0~23)의 숫자로 시간을 구한다 |
getMiliseconds() | 1/1000초까지 구한다 |
getMinutes() | (0~59)의 숫자로 분을 구한다 |
getMonth() | (0~11)의 숫자로 월을 구한다 |
getSeconds() | (0~59)의 값으로 초를 구한다 |
getTime() | 1970년 1월 1일 0시부터 지정된 시간까지 1/1000초로 표현한다 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let toDay = new Date();
document.write(`getTime() : ${toDay.getTime()} <br>`)
document.write(`getDate() : ${toDay.getDate()} <br>`)
document.write(`getDay() : ${toDay.getDay()} <br>`)
document.write(`getFullYear() : ${toDay.getFullYear()} <br>`)
document.write(`getHours() : ${toDay.getHours()} <br>`)
document.write(`getMilliseconds() : ${toDay.getMilliseconds()} <br>`)
document.write(`getMinutes() : ${toDay.getMinutes()} <br>`)
document.write(`getMonth() : ${toDay.getMonth()} <br>`)
document.write(`getSeconds() : ${toDay.getSeconds()} <br>`)
</script>
</body>
</html>
set 메서드
날짜의 일부를 설정하는데 사용된다.
set 메서드 | 설명 |
setDate() | (1~31)의 숫자로 날짜를 설정한다 |
setFullYear() | 4자릿수의 년도를 설정한다 |
setHours() | (0~23)의 숫자로 시간을 설정한다 |
setMiliseconds() | 1/1000초까지 설정한다 |
setMinutes() | (0~59)의 숫자로 분을 설정한다 |
setMonth() | (0~11)의 숫자로 월을 설정한다 |
setSeconds() | (0~59)의 값으로 초를 설정한다 |
setTime() | 1970년 1월 1일 0시부터 지정된 시간까지 1/1000초로 설정한다 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let thisDay = new Date();
thisDay.setFullYear(2024,8,6);
document.write(`setFullYear() : ${thisDay} <br>`)
thisDay.setDate(20);
document.write(`setDate(20) : ${thisDay} <br>`)
thisDay.setDate(thisDay.getDate() + 50);
document.write(`setDate() : ${thisDay} <br>`)
thisDay.setHours(15);
document.write(`setHours() : ${thisDay} <br>`)
thisDay.setMilliseconds(2345);
document.write(`setMilliseconds() : ${thisDay} <br>`)
thisDay.setMinutes(34);
document.write(`setMinutes() : ${thisDay} <br>`)
thisDay.setMonth(7);
document.write(`setMonth() : ${thisDay} <br>`)
thisDay.setTime(840023400000);
document.write(`setTime() : ${thisDay} <br>`)
</script>
</body>
</html>
parse(), now()
parse() 메서드는 유효한 날짜 문자열이 있는 경우 밀리 초로 변환할 수 있고
now() 메서드는 현재 시간에 해당하는 숫자 값(밀리 초)를 반환한다. 두 메서드는 1970년 1월 1일 00:00:00 UTC 이후로 경과된 시간(밀리 초)이다.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let milisec = Date.parse("july 13, 2015");
document.write(milisec + "<br>");
let string_Date = new Date(milisec);
document.write(string_Date + "<br>");
let cursec = Date.now();
document.write(cursec + "<br>");
</script>
</body>
</html>
비교 연산하기
사용자로부터 받은 날짜를 지정된 다른 날짜와 비교할 수 있다. 날짜를 비교할 때, 두 날짜를 1970년 1월 1일 이후의 밀리초로 계산하여 크기를 비교한다. 다음은 2017년 1월 14일 빌려간 cd의 경과 기간을 계산한다.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let today, someday, text;
today = new Date();
someday = new Date();
someday.setFullYear(2014,1,14);
if (someday > today) {
text = "Today is before January 14, 2024";
} else {
text = "Today is after January 14, 2024";
let diff = today.getTime() - someday.getTime();
let del_Day = diff/(1000*60*60*24);
document.write(text + "<br>" + del_Day + "일 경과했습니다.")
}
</script>
</body>
</html>