您的位置:首页 > Web前端 > JavaScript

javaScript计算两个日期相差天数

2016-03-31 14:22 639 查看
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<title></title>

<script type="text/javascript">
function jiSanDays(endDate1){
var nowDate1=new Date();
var nowDate=new Date(nowDate1.getFullYear(),nowDate1.getMonth(),nowDate1.getDate());
var proDate=new Date(parseInt(endDate1.substring(0, 4),10),parseInt(endDate1.substring(4, 6),10)-1,parseInt(endDate1.substring(6, 8),10));
var days=parseInt(Math.abs(proDate -nowDate)/1000/60/60/24)
if(proDate<nowDate){
days=0;
}
document.getElementById("wrapper").innerHTML=(proDate -nowDate)+"毫秒,相差天数"+days;
}

</script>

</head>
<body>

<div id="wrapper">1111</div>
<script>
jiSanDays("20160410")
</script>
</body>
</html>




总结:1、字符串转日期类型:

new Date(nowDate1.getFullYear(),nowDate1.getMonth(),nowDate1.getDate())

2、两个日期类型的相减的到的是毫秒值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: