-4

我有这个代码,这个代码必须能够计算用户的年龄,并且它必须显示在提供的文本框中,如果用户更改了他的出生日期,年龄必须改变。但此代码不起作用,它不会在文本框中显示计算出的年龄。

<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>
4

3 回答 3

2

这是在 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>

于 2019-02-12T05:52:44.080 回答
0

试试这个..

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));
}
于 2013-04-20T23:57:02.353 回答
0

您的代码中有三个错误,请参阅下面的内联注释:

第一个选项的年份值是 991 而不是 1991,可能会导致您认为计算错误。

包含分配给出生变量的日期的字符串必须作为参数传递给 Date() 函数,以创建一个日期对象,该对象可以与它下面的当前日期对象一起使用。

声明了变量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>

假设选择了第一个选项,这将返回以下值:

年龄:0.057534246575342465

计算机年龄:21

您是否也只是想以年、月、日为单位来计算年龄?

于 2013-04-21T00:10:51.610 回答