js计算年龄,js计算距离下次生日的天数

发布时间:2020-03-30编辑:脚本学堂
js计算年龄的一段代码,精确到毫秒,js编写计算年龄工具,根据出生日期计算年龄的例子。

效果图:
js计算年龄

代码:
 

复制代码 代码示例:
<html>
<head>
<title>js编写计算年龄工具 - www.plcxue.com</title>
<script language="javascript">
function js_age_net() {
document.form1.day.value = "";
document.form1.month.value = "";
document.form1.year.value = "";
document.form1.age.value = "";
document.form1.months.value = "";
document.form1.weeks.value = "";
document.form1.answer.value = "";
document.form1.hours.value = "";
document.form1.min.value = "";
document.form1.sec.value = "";
document.form1.bday.value = "";
document.form1.milli.value = "";
}
function run() {
var ap;
dd = document.form1.day.value;
mm = document.form1.month.value;
yy = document.form1.year.value;
with(document.form1) {
ap = parseint(ampm.selectedindex);
hr = parseint(hrs.value);
if(hr <= 0 && hr >= 13) {
ap = null;
alert("asdf")
   }
}
main="valid";
if ((mm < 1) || (mm > 12) || (dd < 1) || (dd > 31) || (yy < 1) ||(mm == "") || (dd == "") || (yy == ""))
main = "invalid";
else
if (((mm == 4) || (mm == 6) || (mm == 9) || (mm == 11)) && (dd > 30))
main = "invalid";
else
if (mm == 2) {
if (dd > 29)
main = "invalid";
else if((dd > 28) && (!lyear(yy)))
main="invalid";
}
else
if((yy > 9999)||(yy < 0))
main = "invalid";
else
main = main;
if(main == "valid") {
function leapyear(a) {
if(((a % 4 == 0) && (a % 100 != 0)) || (a % 400 == 0))
return true;
else
return false;
}
days = new date();
gdate = days.getdate();
gmonth = days.getmonth();
gyear = days.getyear();
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.form1.age.value="您的年龄是:" + age+ "岁 ";
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 = n + 1; }
if (mm == 1) { n = 365 - dd; if (leapyear(yy)) n = n + 1; }
if (gmonth == 1) m = 31;
if (gmonth == 2) { m = 59;   if (leapyear(gyear)) m = m + 1; }
if (gmonth == 3) { m = 90;   if (leapyear(gyear)) m = m + 1; }
if (gmonth == 4) { m = 120;  if (leapyear(gyear)) m = m + 1; }
if (gmonth == 5) { m = 151;  if (leapyear(gyear)) m = m + 1; }
if (gmonth == 6) { m = 181;  if (leapyear(gyear)) m = m + 1; }
if (gmonth == 7) { m = 212;  if (leapyear(gyear)) m = m + 1; }
if (gmonth == 8) { m = 243;  if (leapyear(gyear)) m = m + 1; }
if (gmonth == 9) { m = 273;  if (leapyear(gyear)) m = m + 1; }
if (gmonth == 10) { m = 304; if (leapyear(gyear)) m = m + 1; }
if (gmonth == 11) { m = 334; if (leapyear(gyear)) m = m + 1; }
if (gmonth == 12) { m = 365; if (leapyear(gyear)) m = m + 1; }
totdays = (parseint(age) * 365);
totdays += age / 4;
totdays = parseint(totdays) + gdate + m + n;
document.form1.answer.value = "" + totdays +" 日 ";
months = age * 12;
months += 12 - parseint(mm);
months += gmonth;
document.form1.months.value = months + " 月";
if (gmonth == 1) p = 31 + gdate;
if (gmonth == 2) { p = 59 + gdate;   if (leapyear(gyear)) m = m + 1; }
if (gmonth == 3) { p = 90 + gdate;   if (leapyear(gyear)) p = p + 1; }
if (gmonth == 4) { p = 120 + gdate;  if (leapyear(gyear)) p = p + 1; }
if (gmonth == 5) { p = 151 + gdate;  if (leapyear(gyear)) p = p + 1; }
if (gmonth == 6) { p = 181 + gdate;  if (leapyear(gyear)) p = p + 1; }
if (gmonth == 7) { p = 212 + gdate;  if (leapyear(gyear)) p = p + 1; }
if (gmonth == 8) { p = 243 + gdate;  if (leapyear(gyear)) p = p + 1; }
if (gmonth == 9) { p = 273 + gdate;  if (leapyear(gyear)) p = p + 1; }
if (gmonth == 10) { p = 304 + gdate; if (leapyear(gyear)) p = p + 1; }
if (gmonth == 11) { p = 334 + gdate; if (leapyear(gyear)) p = p + 1; }
if (gmonth == 12) { p = 365 + gdate; if (leapyear(gyear)) p = p + 1; }
weeks = totdays / 7;
weeks += " 星期";
weeks = parseint(weeks);
document.form1.weeks.value = weeks + " 星期";
var time = new date();
ghour = time.gethours();
gmin = time.getminutes();
gsec = time.getseconds();
hour = ((age * 365) + n + p) * 24;
hour += (parseint(age / 4) * 24);
if(ap == 0)
hour = hour - hr;
else {
if(ap == 1) {
hour = hour - (11 + hr)
   }
}
document.form1.hours.value = hour + " 小时";
var min;
min = (hour * 60) + gmin;
document.form1.min.value = min + " 分钟";
sec = (min * 60) + gsec;
document.form1.sec.value = sec + " 秒";
var millisec;
var gmil;
gmil = days.getmilliseconds();
millisec = (sec * 1000) + gmil;
document.form1.milli.value = millisec + " 毫秒";
mm = mm - 1;
var r;
if(mm == 0) r = 1;
if(mm == 1) r = 31;
if(mm == 2) { r = 59;    if (leapyear(gyear)) m = m + 1; }
if(mm == 3) { r = 90;    if (leapyear(gyear)) r = r + 1; }
if(mm == 4) { r = 120;   if (leapyear(gyear)) r = r + 1; }
if(mm == 5) { r = 151;   if (leapyear(gyear)) r = r + 1; }
if(mm == 6) { r = 181;   if (leapyear(gyear)) r = r + 1; }
if(mm == 7) { r = 212;   if (leapyear(gyear)) r = r + 1; }
if(mm == 8) { r = 243;   if (leapyear(gyear)) r = r + 1; }
if(mm == 9) { r = 273;   if (leapyear(gyear)) r = r + 1; }
if(mm == 10) { r = 304;  if (leapyear(gyear)) r = r + 1; }
if(mm == 11) { r = 334;  if (leapyear(gyear)) r = r + 1; }
if(mm == 12) { r = 365;  if (leapyear(gyear)) r = r + 1; }
mm = mm + 1;
r = parseint(r) + parseint(dd);
if( mm > (gmonth + 1)) {
bday = r - m - gdate;
}
else {
if(mm == (gmonth + 1) && (gdate < dd)) {
bday = (r - m - gdate);
}
else {
if((leapyear(gyear)) && ((mm > 2) && (dd < 29))) {
a = 366;
}
else {
a = 365;
}
bday = a + (r - m - gdate);
   }
}
nhour = 24-parseint(ghour);
nmin = 60 - parseint(gmin);
nsec = 60 - parseint(gsec);
go();
if(((bday == 366) && (leapyear(yy))) || ((bday == 365) && (!leapyear(yy)))) {
document.form1.bday.value = "今天是您的生日喔";
alert("happy birthday");
} else {
document.form1.bday.value = bday + " 日 " + nhour + " 小时 " + nmin + " 分钟 " + nsec + " 秒";
settimeout("run()", 1);
}
function go() {
function lyear(a) {
if(((a % 4 == 0) && (a % 100 != 0)) || (a % 400 == 0)) return true;
else return false;
}
mm = parseint(mm);
dd = parseint(dd);
yy = parseint(yy);
if ((mm < 1) || (mm > 12) || (dd < 1) || (dd > 31) || (yy < 1) ||(mm == " ") || (dd == " ") || (yy == " "))  main="invalid";
else
if (((mm == 4) || (mm == 6) || (mm == 9) || (mm == 11)) && (dd > 30)) main = "invalid";
else
if (mm == 2) {
if (dd > 29)
main = "invalid";
else
if(( dd > 28) && (!lyear(yy)))
main = "invalid";
}
else main = main;
if(main == "valid") {
var m;
if (mm == 1) n = 31;
if (mm == 2) n = 59 + 1;
if (mm == 3) n = 90 + 1;
if (mm == 4) n = 120 + 1;
if (mm == 5) n = 151 + 1;
if (mm == 6) n = 181 + 1;
if (mm == 7) n = 212 + 1;
if (mm == 8) n = 243 + 1;
if (mm == 9) n = 273 + 1;
if (mm == 10) n = 304 + 1;
if (mm == 11) n = 334 + 1;
if (mm == 12) n = 365 + 1;
if((mm == 1)||(mm == 3)||(mm == 5)||(mm == 7)||(mm == 8)||(mm == 10)||(mm == 12))
n += 31 + dd;
else if((mm == 4)||(mm == 6)||(mm == 9)||(mm == 11))
n += 31 + dd + 1;
else if(mm == 2) {
if(lyear(yy)) n += 29 + dd - 3;
else if(!lyear(yy)) n += 28 + dd - 1;
}
fours = yy / 4;
hunds = yy / 100;
fhunds = yy / 400;
var day;
day = (yy + n + fours - hunds + fhunds) % 7;
day = parseint(day)
switch(day)
{
case 1 : document.form1.age.value +="  您出生于 星期天"
break
case 2 : document.form1.age.value +="  您出生于 星期一"
break
case 3 : document.form1.age.value +="  您出生于 星期二"
break
case 4 : document.form1.age.value +="  您出生于 星期三"
break
case 5 : document.form1.age.value +="  您出生于 星期四"
break
case 6 : document.form1.age.value +="  您出生于 星期五"
break
case 7 : document.form1.age.value +="  您出生于 星期六"
break
case 0 : document.form1.age.value +="  您出生於 星期六"
break
   }
}
else {
document.form1.age.value += main + " 日";
      }
   }
}
else {
document.form1.age.value = main + " 日";
document.form1.months.value = "";
document.form1.weeks.value = "";
document.form1.answer.value = "";
document.form1.hours.value = "";
document.form1.min.value = "";
document.form1.sec.value = "";
document.form1.bday.value = "";
document.form1.milli.value = "";
   }
}
//  end -->
</script>
</head>
<body onload="js_age_net()">
 请输入您的出生日期:<br>
<form name=form1>
    月
<input name=month size=3>
     日
    <input name=day size=3>
     公元
<input name=year size=6>
     时间<input name=hrs value=00 size=6>
<select size="1" name="ampm">
<option selected>am</option>
<option>pm</option>
</select>
<input name=js_age_net onclick=run() type=button value="计算">
<br>
<br>
<input name=age size=55 value="您的年龄">
<br>
<br>
出生到现在您已过了如下表列之时间<br>
<table border = 0>
<tbody>
<tr>
        <td>以月计算</td>
<td><input name=months size=30></td>
</tr>
<tr>
        <td>以星期计算</td>
<td><input name=weeks size=30></td>
</tr>
<tr>
        <td>以日期计算</td>
<td><input name=answer size=30></td>
</tr>
<tr>
        <td>以小时计算</td>
<td><input name=hours size=30></td>
</tr>
<tr>
        <td>以分钟计算</td>
<td><input name=min size=30></td>
</tr>
<tr>
        <td>以秒計算</td>
<td><input name=sec size=30></td>
</tr>
<tr>
        <td>以毫秒計算</td>
<td><input name=milli size=30></td>
</tr>
</tbody>
</table>
到您的下一次生日还剩下<br>
<input name=bday size=40>
<h1>珍惜
生命、爱护时间!!!</h1>
</form>
</body>
</html>