JS 计算天数示例代码

发布时间:2019-10-05编辑:脚本学堂
分享一个js计算天数的实现代码,js时间计算、天数计算的小例子,有需要的朋友参考下。

例子,js天数计算。
 

复制代码 代码示例:
<script type="text/javascript">
function test(str){  
 var dateStr = str;
 var date = new Date(dateStr);
 var now = new Date();
 
 var mis = now.getTime() - date.getTime();
 var daysCount = 24 * 60 * 60 * 1000;
 var days = parseInt(mis/daysCount);
 //m(days);
 var mis = mis % daysCount;
 var hoursCount = daysCount /24;
 var hours = parseInt(mis/hoursCount);
 var mis = mis % hoursCount ;
 var minsCount = hoursCount / 60;
 var mins = parseInt(mis/minsCount);
 var mis = mis % minsCount;
 var sesCount = minsCount /60;
 var ses = parseInt(mis/sesCount);
 var msg = days>0 ? '已经有' : '还有'; 
 days = days>0 ?days : days /-1;
 days=days+1;
 hours = hours>0 ?hours : hours /-1;
 mins = mins>0 ?mins : mins /-1;
 ses = ses>0 ?ses : ses /-1;
 var shijian  = "与你已认识"+ msg + days + '天' + hours + '小时' + mins + '分' + ses + '秒';
   alert(shijian)
}
test("2011/11/20 8:30:00");//----距离时间---
</script>