<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head> <body>
<script>
function daysBetween(DateOne,DateTwo)
{
var OneMonth = DateOne.substring(,DateOne.lastIndexOf ('-'));
var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+);
var OneYear = DateOne.substring(,DateOne.indexOf ('-'));
var TwoMonth = DateTwo.substring(,DateTwo.lastIndexOf ('-'));
var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+);
var TwoYear = DateTwo.substring(,DateTwo.indexOf ('-')); //算年数
var newYear;
if(OneMonth-TwoMonth>= && OneDay-TwoDay>=){
newYear=OneYear-TwoYear
}
if(OneMonth-TwoMonth< ){
newYear=OneYear-TwoYear-
} //算月数
var newmonth;
if(OneMonth-TwoMonth>= && OneDay-TwoDay>=){
newmonth=OneMonth-TwoMonth;
newYear=OneYear-TwoYear;
}else if(OneMonth-TwoMonth>= && OneDay-TwoDay<){
newmonth=OneMonth-TwoMonth-;
newYear=OneYear-TwoYear
}else if(OneMonth-TwoMonth< && OneDay-TwoDay>=){
newmonth=OneMonth-TwoMonth+;
newYear=OneYear-TwoYear-
}
else{
newmonth=OneMonth-TwoMonth+
newYear=OneYear-TwoYear-
}
//alert(newmonth) //算天数
var newday;
if(OneDay-TwoDay>=){
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();
return curDate.getDate();
}
window.onload=function(){
var oDiv1=document.getElementById("div1");
var data=new Date();
var theyear=data.getFullYear();
var themouth=data.getMonth()+;
var theday=data.getDate();
var str=theyear+'-'+themouth+'-'+theday
oDiv1.innerHTML=daysBetween(str,'1999-1-15')
} </script>
<div id="div1"></div> </body>
</html>