Home >Web Front-end >JS Tutorial >Javascript implements date difference calculation

Javascript implements date difference calculation

巴扎黑
巴扎黑Original
2017-08-18 10:46:001417browse

This article mainly introduces the example explanation of javascript date subtraction. The code is attached for everyone to view. Friends in need can refer to it

The code is as follows:


<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>

The above is the detailed content of Javascript implements date difference calculation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn