`
yjgyjg4
  • 浏览: 115904 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

js日期相减

阅读更多
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>

<script language="javascript1.1" type="text/javascript">
	 
<!--  
//说明:这里用了Math.ceil()函数,向上取整,即零头算一天,^_^  
/*var  strDate1  =  "2009-08-31 23:00:00.0";  
var  strDate2  =  "2009-09-01 00:00: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相差"+Math.ceil((date2-date1)/(24*60*60*1000))+"天")  
 */
var  strDate_1  = "2009-08-30 23:00:00.0";  
var  strDate_2  = "2009-09-01 00:00:00.0";  
strDate_1=strDate_1.substring(0,strDate_1.lastIndexOf(".")).replace(/-/g,"/");  
strDate_2=strDate_2.substring(0,strDate_2.lastIndexOf(".")).replace(/-/g,"/");  
var  date1  =  Date.parse(strDate_1);  
var  date2  =  Date.parse(strDate_2);  
alert("strDate2与strDate1相差"+(date2-date1)/(60*60*1000)+"小时")  
//-->  
</script>
</head>

<body>
</body>
</html>

引用
上面可以获得小时,和天数

引用
下面日期格式yyyy-MM-dd hh:mm


function comptime(beginTime,endTime){

var beginTimes=beginTime.substring(0,10).split('-');
var endTimes=endTime.substring(0,10).split('-');

beginTime=beginTimes[1]+'-'+beginTimes[2]+'-'+beginTimes[0]+' '+beginTime.substring(10,19);
endTime=endTimes[1]+'-'+endTimes[2]+'-'+endTimes[0]+' '+endTime.substring(10,19);

var a =(Date.parse(endTime)-Date.parse(beginTime))/3600/1000;
return a;
} 

引用
下面日期格式yyyy-MM-dd

function DateDiff(sDate1, sDate2){
     var aDate, oDate1, oDate2, iDays 
     aDate = sDate1.split("-") 
     oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]) 
     aDate = sDate2.split("-") 
     oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]) 
     iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24) 
     return iDays 
} 
分享到:
评论
1 楼 zhangzi 2010-04-18  
如何获得准确的 相差几个月零几天?

相关推荐

Global site tag (gtag.js) - Google Analytics