简体   繁体   English

Javascript 中的年龄计算器

[英]Age Calculator in Javascript

I have this codes, this code must be able to compute the age of the user and It must be displayed on the text box provided and the age must change if the user changed his birth-date.我有这个代码,这个代码必须能够计算用户的年龄,它必须显示在提供的文本框中,如果用户更改了他的出生日期,年龄必须更改。 but this code does not work, it doesn't display the computed age in the textbox.但此代码不起作用,它不会在文本框中显示计算的年龄。

<input name= "date" type="text" readonly="readonly"  />

<select id="Ultra" onchange="run()">  
 <option value="11/15/991">1991-11-15</option>
 <option value="10/23/1992">1992-10-23</option>
</select><br><br>
TextBox1<br>
 <input type="text" id="srt" placeholder="get value on option select"   readonly="readonly"><br>
<script type="text/javascript">
function run() {
  var birth = document.getElementById("Ultra").value;
  var check = new Date();
  var milliDay = 1000 * 60 * 60 * 24;

  var AgeinDay = (check - birth) / milliday;
  var ComputAge = Math.floor(AgeinDay / 365 );
  var age = ComputAge / 365;
  document.getElementById("srt").value = age;
}
 </script>

Here is a look to complete Age Calculation in JavaScript:下面是在 JavaScript 中完成年龄计算的外观:

 <body onload="getAge()"> <h1 id="age" ></h1> <script> function calculateAge(dob) { var now = new Date(); var dob = new Date(dob); var year=now.getYear()-dob.getYear(); var month=now.getMonth()-dob.getMonth(); if(month<0){ month=now.getMonth()+12-dob.getMonth(); year=year-1; } var day=now.getDate()-dob.getDate(); if(day<0){ var monthNumber=dob.getMonth(); var fullDate=getFullDate(monthNumber); day=now.getDate()+fullDate-dob.getDate(); month=month-1; } return year+" Years, "+month+" Months, "+day+" Days!"; }; function getFullDate(x){ switch(x){ case 0: return 31; break; case 1: return 28; break; case 2: return 31; break; case 3: return 30; break; case 4: return 31; break; case 5: return 30; break; case 6: return 31; break; case 7: return 31; break; case 8: return 30; break; case 9: return 31; break; case 10: return 30; break; case 11: return 31; } } function getAge(){ x=prompt("Please Enter Your Date of Birth in format (yyyy-mm-dd): ",""); x=new Date(x); document.getElementById("age").innerHTML="Your age is: "+calculateAge(x); } </script> </body>

try this..试试这个..

function run() {
  var birth = new Date(document.getElementById("Ultra").value);
  var curr  = new Date();
  var diff = curr.getTime() - birth.getTime();
   document.getElementById("srt").value = Math.floor(diff / (1000 * 60 * 60 * 24 * 365.25));
}

There were three errors in your code, see the comments inline below:您的代码中存在三个错误,请参阅下面的内联注释:

The year value of first option was 991 instead of 1991, might cause you to think the calculation is wrong.第一个选项的年份值是 991 而不是 1991,可能会导致您认为计算错误。

String containing date that is being assigned to birth variable has to be passed as parameter to Date() function to create a date object that can be used with the current date object below it.包含分配给出生变量的日期的字符串必须作为参数传递给 Date() 函数以创建一个日期对象,该对象可以与它下面的当前日期对象一起使用。

Variable milliDay was declared, then you were trying to use milliday (wrong case D).变量milliDay 已声明,然后您尝试使用milliday(错误的情况D)。

<input name= "date" type="text" readonly="readonly"  />

<select id="Ultra" onchange="run()">  
 <option value="11/15/1991">1991-11-15</option> <!-- year value was 991 instead of 1991, might cause you to think the calculation is wrong -->
 <option value="10/23/1992">1992-10-23</option>
</select><br><br>
TextBox1<br>
 <input type="text" id="srt" placeholder="get value on option select"   readonly="readonly"><br>
<script type="text/javascript">
function run() {
  var birth = new Date(document.getElementById("Ultra").value); //string containing date has to be passed as parameter to Date() function to create a date object that can be used with the current date object below
  var check = new Date();
  var milliDay = 1000 * 60 * 60 * 24;

  var AgeinDay = (check - birth) / milliDay; //variable here was milliday all small case, declared above as milliDay with a capital D
  var ComputAge = Math.floor(AgeinDay / 365 );
  var age = ComputAge / 365;
  document.getElementById("srt").value = age;
}
 </script>

This will return the following values assuming the first option is selected:假设选择了第一个选项,这将返回以下值:

age: 0.057534246575342465年龄:0.057534246575342465

ComputAge: 21计算年龄:21

Are you just trying to get the age in years, or months, days hours too?您是否只是想以年、月、日为单位计算年龄?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM