Date 객체 경과 시간

조수경·2021년 11월 18일
0

HTML

목록 보기
56/96
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel = "stylesheet" href = "../css/mystyle.css" type="text/css">

<script>
today = new Date();

function proc1(){
  bir = new Date(2010, 4-1, 12);	
  //bir = new Date("2010.4.12");
  
  ttime = today.getTime();//millisec
  btime = bir.getTime();//millisec
  console.log(ttime, btime);
  
  times = ttime - btime; //millisec
  
  res = times /1000 /60 /60 /24 /365;//1000으로 초 시 분 1년으로 시간을 나눔 
  console.log(res);
  
  str = "오늘 : " + today.toLocaleDateString() + "<br>"
  str += "태어난 날:" +  bir.toLocaleDateString() + "<br>"
  str += parseInt(Math.ceil(res)) + "경과 하였습니다"
  
  document.querySelector('#result1').innerHTML = str;
  
}

function proc2(){ 
	tomilli = today.getTime();
	milli100 = 1000* 60* 60* 24* 100;
	
	millisum = tomilli + milli100;
	
	after100 = new Date(millisum);
	
	res = after100.toLocaleDateString();
	
	str = "오늘 : " + today.toLocaleDateString() + "<br>";
	str += "100일 후 : " + after100.toLocaleDateString() + "<br>";
	
	document.querySelector('#result2').innerHTML = str;
}

function proc3(){
	// 1. id = pdate인 요소의 선택한 값을 가져온다 - value - "2020.11.4" - String이다.
	datevalue = document.getElementById('pdate').value;
	
	// 2. 1번의 value값으로 Date객체를 생성한다.
	 selDate = new Date(datevalue);
	
	// 경과시간
	// 3. 오늘의 getTime - 2번의 getTime //결과 값은 millisec
	times = today.getTime() - selDate.getTime();
	console.log("times=" + times);
	if(times < 0) {
		alert("선택 오류 입니다");
	      return false;
	}
	// 4.경과한 날 수를 구한다 - times /1000 /60 /60 /24 
	days = times /1000 /60 /60 /24;
	console.log("days=" + days);
	
	// 5. 4번의 결과값을 비교
	if(days > 7){
		res = "교환불가능";
	}else{
		res = "교환가능";
	}
	
	// 5. result3에 출력 - toLocaldateString()
    // 오늘 날짜 - today.toLocaleDateString()
    // 구입 날짜 - 2번의.toLocaleDateString()
    // 결과 res
	str = "오늘 : " + today.toLocaleDateString() + "<br>";
	str += "구입 날짜 : " + selDate.toLocaleDateString() + "<br>";
	str += res ;
	
	document.querySelector('#result3').innerHTML = str;
	
}

function proc4(){
	  chris = new Date("2021.12.25");	
	  
	  //chris millisec - 오늘의 millisec
	  times = chris.getTime() - today.getTime(); //millisec
	  
	  //날 수 구하기
	  days = parseInt(times /1000 /60 /60 /24);  //1000으로 초 시 분 1년으로 시간을 나눔 
	    
	  str = "크리스마스 까지" + days + "일 남았습니다."
	  
	  document.querySelector('#result4').innerHTML = str;
	
	
}
</script>

</head>
<body>

<div class = "box">
    경과시간 구하기
  getTime() - 리턴값은 millisec이다<br>
  millisec를 1000으로 나누면 실제 초를 구할 수 있다.<br>
     태어난날 로부터 현재까지 일 또는 년수 구하기
   <br>
  <button type = "button" onclick="proc1()">확인</button>
  <div id = "result1"></div>
</div>


<div class = "box">
    경과시간 구하기
    100일 후의 날짜 구하기
    getTime() - 리턴값은 millisec이다<br>
    1일의 millisec -> 1000* 60* 60* 24<br>
    a = 현재의 millisec + 100일 후의 millisec<br>
    new Date(a)<br>
   
   <br>
  <button type = "button" onclick="proc2()">확인</button>
  <div id = "result2"></div>
</div>

<div class = "box">
    경과시간 구하기
        선택한 날로부터 경과한 날 수 구하기
    getTime() - 리턴값은 millisec이다<br> 
    7일 이상이면 교환반품 불가능 아니면 가능 <br>
   <br>   
   <input type = "date" id = "pdate"><br>
  <button type = "button" onclick="proc3()">확인</button>
  <div id = "result3"></div>
</div>

<div class = "box">
    경과시간 구하기  getTime() - 리턴값은 millisec이다<br> 
        크리스마스까지 남은 일수 구하기
        크리스마스 millisec - 오늘의millisec한다<br> 
    
    7일 이상이면 교환반품 불가능 아니면 가능 <br>
   <br>   
   <input type = "date" id = "pdate"><br>
  <button type = "button" onclick="proc4()">확인</button>
  <div id = "result4"></div>
</div>


</body>
</html>

profile
신입 개발자 입니다!!!

0개의 댓글