JavaScript中得到日期间隔

    <script type="text/javascript" language="javascript">         function GetDateTime()         {            var vBeginDate=document.all["txbIntendBegin"].value;            var vEndDate=document.all["txbIntendEnd"].value;            if(vBeginDate!=null && vEndDate!=null)            {                 var dBegin=vBeginDate.split(".");                 var dEnd=vEndDate.split(".");                 var dtBegin=new Date(dBegin[0],dBegin[1],dBegin[2]);                 var dtEnd=new Date(dEnd[0],dEnd[1],dEnd[2]);           //得到间隔天数,去掉24即得到间隔小时                var TimeSpan=(dtEnd-dtBegin)/(24*60*60*1000);                if(TimeSpan<0)                 {                       //检查                    alert("截至日期必须大于起始日期!");                     document.all["txbIntendEnd"].focus();                     return false;                 }                 else                 {                     var vWorkLoad= document.getElementById("txbIntendWorkload");                     if(vWorkLoad!=null)                     {                         //界面中显示间隔(天)                        vWorkLoad.value=TimeSpan.toString();                     }                 }            }             else            {              window.alert("没有资料");                return false;            }            return true;                    }

 

网上上也有其它的方法,代码如下:

<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>
26人参与, 0条评论 登录后显示评论回复

你需要登录后才能评论 登录/ 注册