<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h2>輸入<input type="date" id="usertype"><button id="btn">所以結果是</button>
<br>
這天離現在共有</h2>
<h2><span id="differday"></span>天</h2>
<script type="text/javascript">
document.getElementById('btn').onclick=function(){
var usertype=document.getElementById('usertype').value;
var usertypetoobj=new Date(usertype);
var today=new Date();
var difference=today.getTime()-usertypetoobj.getTime();
/*毫秒數換算*/
var howManyDay=Math.floor(difference/(1000*60*60*24));
document.getElementById('differday').textContent=howManyDay;
}
</script>
</body>
</html>
2017年8月31日 星期四
getTime出來的單位是毫秒數(某日期離現在共幾天)
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言