这篇文章主要介绍了javascript 日期相减的实例讲解,附上代码供大家查看,需要的朋友可以参考下
代码如下:
<SCRIPT LANGUAGE="JavaScript">
<!--
//说明:这里用了Math.ceil()函数,向上取整,即零头算一天,^_^
var strDate1 = "2003-06-17 03:03:40.0";
var strDate2 = "2004-09-18 12:03:12.0";
strDate1=strDate1.substring(0,strDate1.lastIndexOf(".")).replace(/-/g,"/");
strDate2=strDate2.substring(0,strDate2.lastIndexOf(".")).replace(/-/g,"/");
var date1 = Date.parse(strDate1);
var date2 = Date.parse(strDate2);
alert("strDate2与strDate1相差"+Math.ceil((date2-date1)/(24*60*60*1000))+"天")
//-->
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
<!--
var strDate1 = "2004-09-17 03:03:00.0";
var strDate2 = "2004-09-17 04:05:00.0";
strDate1=strDate1.substring(0,strDate1.lastIndexOf(".")).replace(/-/g,"/");
strDate2=strDate2.substring(0,strDate2.lastIndexOf(".")).replace(/-/g,"/");
var date1 = Date.parse(strDate1);
var date2 = Date.parse(strDate2);
alert("strDate2与strDate1相差"+(date2-date1)/(60*60*1000)+"小时")
//-->
</SCRIPT>以上就是javascript实现日期差计算的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号