日期:2014-05-16  浏览次数:20342 次

JavaScript 计算日期间隔,生日,年龄

最近些项目的时候被一个小问题难住了.就是javascript计算日期间隔的问题,还有就是生日的计算

下面是从网上搜到的:

?

新建一个html,把下面的代码贴到html文件里,保存后用浏览器打开

?

1.下面的这个算法并不精确,有待高手指点.

--------------------------------------------
<Html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title></title>

</head>

<body>

<script language="JavaScript">

? function btnCount_Click(){

??? s1 = document.getElementById('sdate1').value;

??? s2 = document.getElementById('sdate2').value;

??? DateDiff(s1,s2);

? }

? //计算天数差的函数,通用

? function DateDiff(sDate1, sDate2){? //sDate1和sDate2是2002-12-18格式

??? var aDate, oDate1, oDate2, iDays

??? aDate = sDate1.split("-")

??? oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])? //转换为12-18-2002格式

??? aDate = sDate2.split("-")

??? oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])

??? iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24)? //把相差的毫秒数转换为天数
???
??? month = Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24 /30
???
??? year = Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24 /365//误差就在这里,这个地方把一年当成了365天

???
??? alert('Day:'+iDays+' Month:'+month+' Years:'+ year +'\nage:' + Math.floor(year));

??? return iDays

? }

</script>

<input type = 'text' id = 'sdate1' />
<br>
<input type = 'text' id = 'sdate2' />
<br>
<button onClick=" btnCount_Click()">第一种计算</button><br>

</body>


--------------------------------------------

下面是第二种计算方法(推荐):


--------------------------------------------

<body>
<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
function run() {
with (document.agecalc) {
dd = parseInt(day.selectedIndex) + 1;
mm = parseInt(month.selectedIndex) + 1;
yy = year.value;
if (yy.length != 4 || isNaN(yy)) {
document.agecalc.timealive.value = "年份数请输入四位.";
document.agecalc.year.select();
document.agecalc.year.focus();
return;
??? }
}
days = new Date();
gdate = days.getDate();
gmonth = days.getMonth();
gyear = days.getYear();
if (gyear < 2000) gyear += 1900;
age = gyear - yy;
if ((mm == (gmonth + 1)) && (dd <= parseInt(gdate))) {
age = age;
} else {
if (mm <= (gmonth)) {
age = age;
} else {
age = age - 1;
??? }
}
if (age == 0)
age = age;
document.agecalc.timealive.value = "你已经" + age+ " 了 . . .\n\n";
if (mm <= (gmonth + 1))
age = age - 1;
if ((mm == (gmonth + 1)) && (dd > parseInt(gdate)))
age = age + 1;
var m;
var n;
if (mm == 12) n = 31 - dd;
if (mm == 11) n = 61 - dd;
if (mm == 10) n = 92 - dd;
if (mm == 9) n = 122 - dd;
if (mm == 8) n = 153 - dd;
if (mm == 7) n = 184 - dd;
if (mm == 6) n = 214 - dd;
if (mm == 5) n = 245 - dd;
if (mm == 4) n = 275 - dd;
if (mm == 3) n = 306 - dd;
if (mm == 2) { n = 334 - dd; if (leapyear(yy)) n++; }
if (mm == 1) { n = 365 - dd; if (leapyear(yy)) n++; }
if (gmonth == 1) m = 31;
if (gmonth == 2) {
m = 59;
if (leapyear(gyear)) m++;
}
if (gmonth == 3)?? { m = 90;?? if (leapyear(gyear)) m++; }
if (gmonth == 4)?? { m = 120; if (leapyear(gyear)) m++; }
if (gmonth == 5)?? { m = 151; if (leapyear(gyear)) m++; }
if (gmonth == 6)?? { m = 181; if (leapyear(gyear)) m++; }
if (gmonth == 7)?? { m = 212; if (leapyear(gyear)) m++; }
if (gmonth == 8)?? { m = 243; if (leapyear(gyear)) m++; }
if (gmonth == 9)?? { m = 273; if (leapyear(gyear)) m++; }
if (gmonth == 10) { m = 304; if (leapyear(gyear)) m++; }
if (gmonth == 11) { m = 334; if (leapyear(gyear)) m++; }
if (gmonth == 12) { m = 365; if (leapyear(gyear)) m++;