Date()에 대하여,
기본적으로 Date를 어떻게 쓰는지를 알아야됩니다.
지금부터 설명해드릴게요.
//1월-12월: 0-11이다.
//시간: 0-23, 0-59, 0-59
//1,31은 => 2월 31일이니까 => 3월 3일이다.
첫번째. Date사용을 위해서, new를 이용해서 객체를 생성해 줍니다.
=> var now = new Date();
두번째. 내장기능들이 어떤게 있는지를 알아야지 써먹을 수 있습니다. 굳이 외울 필요는 없지만, 객체.메서드를 통해서 나오는 값들을 아래를 통해서 어느정도 눈에 익혀두시기 바랍니다.
document.write(b+ now) | Sat Apr 14 2018 21:20:19 GMT+0900 (대한민국 표준시) |
document.write(now.toString()) | Sat Apr 14 2018 21:20:19 GMT+0900 (대한민국 표준시) |
document.write(now.toDateString()) | Sat Apr 14 2018 |
document.write(now.toTimeString()) | 21:20:19 GMT+0900 (대한민국 표준시) |
document.write(now.toLocaleDateString()) | 2018년 4월 14일 |
document.write(now.getFullYear()+"년") | 2018년 |
document.write(now.getMonth()+"월") | 3월 |
document.write(now.getDate()+"일") | 14일 |
document.write(now.getHours()+"시") | 21시 |
document.write(now.getMinutes()+"분") | 20분 |
document.write(now.getSeconds()+"초") | 19초 |
세번째. 객체생성시 동시에 초기화를 할 수 있다.
var date1= new Date(2018,1,31,12,34,56); document.write(date1.toLocaleString()); |
결과값은 2018년 3월 3일 오후 12:34:56 이나온다.
문제1) 배열을 이용한 문제를 풀어볼게요
var day=['일','월','화','수','목','금','토'];
document.write(day[now.getDay()]+"요일"); ==> 현재 작성일은 토요일이에요! 14=> "토"요일이 나옵니다.
문제2) 이제 본격적으로, Date()를 이용한 도서 반납 프로그램을 작성할게요.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<body>
<input type="datetime-local" id="input">
<input type="button" value="유효검사" onclick="check()">
<script>
function check(){
//날짜 시간 입력 정보 //value에 날짜값이 저장되있다.
var time = document.getElementById("input").value; //value값을 time저장
var b = new Date(time); // 도서 반납한 일 : b
var a = new Date(); // 현재일 a
var diff = b.getTime()-a.getTime(); //차이
diff= Math.round(diff/(1000*60*60*24)); //Date객체는 1/1000초 단위
alert(diff);//일 단위
if(diff>0){
document.write("반납일이 "+pasetInt(diff)+'일 남았습니다');
}
else if(diff==0 || diff==-1){
document.write("오늘이 반납일 입니다.");
}
else{
document.write('반납일이'+parseInt(diff)+'일 경과했습니다');
}
document.write("<h1>"+ b.toLocaleDateString()+ "</h1>");
}
</script>
</body>
|
'WEB 기초 > Script' 카테고리의 다른 글
13. 타이머란? (+ 게시판의 입력을 타이머로 응용) (0) | 2018.04.12 |
---|---|
12. 객체란? (0) | 2018.04.12 |
10. 임의의 수를 찾는 프로그램 (0) | 2018.04.12 |
9. 함수란, (변수의 종류까지 정리) (0) | 2018.04.12 |
8. 구구단 구하기 (테이블 이용) (0) | 2018.04.12 |