码迷,mamicode.com
首页 > Web开发 > 详细

计算时间间隔的js

时间:2014-06-11 11:32:45      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   http   

bubuko.com,布布扣
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>

<body>
<script>
function daysBetween(DateOne,DateTwo) 
{  
    var OneMonth = DateOne.substring(5,DateOne.lastIndexOf (‘-‘)); 
    var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf (‘-‘)+1); 
    var OneYear = DateOne.substring(0,DateOne.indexOf (‘-‘));
    var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf (‘-‘)); 
    var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf (‘-‘)+1); 
    var TwoYear = DateTwo.substring(0,DateTwo.indexOf (‘-‘));
    
    //算年数
    var newYear;
    if(OneMonth-TwoMonth>=0 && OneDay-TwoDay>=0){
        newYear=OneYear-TwoYear
    }
    if(OneMonth-TwoMonth<0 ){
        newYear=OneYear-TwoYear-1    
    }
    
    
    //算月数
    var newmonth;
    if(OneMonth-TwoMonth>=0 && OneDay-TwoDay>=0){
        newmonth=OneMonth-TwoMonth;
        newYear=OneYear-TwoYear;
    }else if(OneMonth-TwoMonth>=0 && OneDay-TwoDay<0){
        newmonth=OneMonth-TwoMonth-1;
        newYear=OneYear-TwoYear    
    }else if(OneMonth-TwoMonth<0 && OneDay-TwoDay>=0){
        newmonth=OneMonth-TwoMonth+12;
        newYear=OneYear-TwoYear-1        
    }
    else{
            newmonth=OneMonth-TwoMonth+11
            newYear=OneYear-TwoYear-1    
    }
    //alert(newmonth)
    
    
    //算天数
    var newday;
    if(OneDay-TwoDay>=0){
        newday=    OneDay-TwoDay;
    }
    else{
        newday=parseInt(getCountDays(TwoMonth))+parseInt(OneDay)-TwoDay;    
        
    }
    return newYear+‘年‘+newmonth+‘个月‘+newday+‘天‘;
}

function getCountDays(num) {
      var curDate = new Date();
      curDate.setMonth(num);
      curDate.setDate(0);
      return curDate.getDate();
}
window.onload=function(){
    var oDiv1=document.getElementById("div1");
    var data=new Date();
    var theyear=data.getFullYear();
    var themouth=data.getMonth()+1;
    var theday=data.getDate();
    var str=theyear+‘-‘+themouth+‘-‘+theday
    oDiv1.innerHTML=daysBetween(str,‘1999-1-15‘)
}

</script>
<div id="div1"></div>

</body>
</html>
bubuko.com,布布扣

 

计算时间间隔的js,布布扣,bubuko.com

计算时间间隔的js

标签:style   class   blog   code   java   http   

原文地址:http://www.cnblogs.com/wanliyuan/p/3772725.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!