年龄计算功能

时间:2015-11-19 17:34:46

标签: javascript function

我试图计算月,日,小时和分钟(包括闰年)的用户年龄。。但是,我没有在HTML中使用select /选项,而是通过Javascript输入选项。因此,我现在对如何在Javascript中进行计算感到困惑。有人能指出我正确的方向进行这些计算吗?

HTML:

`  <ul class = "mainList">
    <h1>
      Age Calculator
    </h1>
    <h2> Enter Your Birthday</h2>
  <li class = "firstList">
    <p class = "datePara">
      Date:
    </p>
    <select id = "day">
    </select>
  </li>
  <li>
    <p class = "monthPara">
      Month:
    </p>
    <select id = "month">
    </select>
  </li>
  <li>
    <p class = "yearPara">
      Year:
    </p>
    <select id = "year">
    </select>
  </li>
  </ul>`

Javascript:`

var startyear = "1950";
var endyear = "2015";
var dat = new date();
var curday = dat.getDate();
var curmon = dat.getMonth() + 1;
var curyear = dat.getFullYear();


function initForm() {

  document.getElementById('day').focus();

for (var i = 1; i <= 31; i ++) {

  var thisDay = document.getElementById('day');
  var dayOption = document.createElement('option');

  dayOption.text = i;
  thisDay.add(dayOption, i);
}

for (var i = 1; i <= 12; i ++) {

  var thisMonth = document.getElementById('month');
  var monthOption = document.createElement('option');

  monthOption.text = i;
  thisMonth.add(monthOption, i);
}

for (var i = startyear; i <= endyear; i ++) {

  var thisYear = document.getElementById('year');
  var yearOption = document.createElement('option');

  yearOption.text = i;
  thisYear.add(yearOption, i);
}
}`

2 个答案:

答案 0 :(得分:0)

MomentJS有一个fromNow()函数可以自动处理,处理闰年/本地时间/等。它很容易使用;您只需从用户输入并调用moment.fromNow()时实例化“时刻”。

编辑:没有时刻的选择:

day=document.getElementById('day').value;
month=document.getElementById('month').value;
year=document.getElementById('year').value;
birthdate=new Date(year, month-1, day); // 'month' is zero-indexed.
today = new Date();
age = (today-birthdate) / (1000*60*60*24*365.25);
// /1000 = seconds, /60=minutes, /60=hours, /24=days, /365.25=years, factor leap years

编辑:我意识到你将要实现自己,但无论如何这里有一个简单的例子:

day=document.getElementById('day').value;
month=document.getElementById('month').value;
year=document.getElementById('year').value;
birthdate=new Date(year, month-1, day); // 'month' is zero-indexed.
today = new Date();
age = moment(today).diff(moment(birthdate), 'year');

您可以跳过moment,只记下日期对象(today-birthdate)之间的差异,这将为您提供以毫秒为单位的时间间隔。使用moment的优点是时间间隔是正确的,同时考虑闰年和间隔的开始日期。

答案 1 :(得分:0)

为了计算这样的事情,你想从两个日期的年,月,日等之间的差异开始:

var yearsBetween = currentDate.getYear() - targetDate.getYear();
var monthsBetween = currentDate.getMonth() - targetDate.getMonth();
var daysBetween = currentDate.getDate() - targetDate.getDate();
var hoursBetween = currentDate.getHours() - targetDate.getHours();
var minutesBetween = currentDate.getMinutes() - targetDate.getMinutes();
var secondsBetween = currentDate.getSeconds() - targetDate.getSeconds();

但其中一些价值观将是负面的。在这些情况下,您需要调整次高单位值以进行补偿:

if(secondsBetween < 0) {
  secondsBetween += 60;
  minutesBetween -= 1;
}
if(minutesBetween < 0) {
  minutesBetween += 60;
  hoursBetween -= 1;
}
...
if(daysBetween < 0) {
  // http://stackoverflow.com/a/1184359/120955
  var daysInTargetMonth = new Date(targetDate.getYear(), targetDate.getMonth() + 1, 0).getDate();
  daysBetween += daysInTargetMonth;
  monthsBetween -= 1;
}

然后,最后,听起来你并不关心岁月,所以你想要把它们推到几个月:

monthsBetween += yearsBetween * 12;