使用javascript计算年龄

时间:2015-12-28 19:54:54

标签: javascript

我想计算任何事情的年龄。我已经写了这个程序。我拿了一个日期对象并为其赋值$birth_date=new Date('Dec,15,1992');然后我用当前数据减去该值。根据减法它应该返回23年0个月14天。但它返回23年4个月14天。年日子还可以,但月份误导了。

你会告诉我为什么会出现这种奇怪的结果吗?

以下代码是HTML,旁边是Javascript

$(document).ready(function(){
var birth_date = new Date('Dec, 15, 1992').getTime();

var years,months,days, hours, minutes, seconds;
var ageCount = document.getElementById('counter');
setInterval(function(){

	var current_date = new Date().getTime();
	var total_sec =(current_date-birth_date) / 1000;
	years=parseInt(total_sec/(86400*30*12));
	var second_left=total_sec%(86400*30*12);
	months=parseInt(second_left/(86400*30));
	second_left=second_left%(86400*30);
	days=parseInt(second_left/86400);
	second_left=second_left%(86400);
	hours=parseInt(second_left/3600);
	second_left=second_left%3600;
	minutes=parseInt(second_left/60);
	seconds=parseInt(second_left%60);


	 ageCount.innerHTML=years+' Years '+months+' Months '+days+' Days '+hours+
	' Hours '+minutes+' Minutes '+seconds+' Seconds';

},500);

});
<div id="counter">
	
</div>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>

3 个答案:

答案 0 :(得分:0)

我认为仅使用列出here的基本符号来获取日期差异,日期,小时,分钟,秒等指标会更有意义

$(document).ready(function(){
var birth_date = new Date('Feb, 15, 1992');

var years,months,days, hours, minutes, seconds;
var ageCount = document.getElementById('counter');
setInterval(function(){

var current_date = new Date();
var YearDiff =  (current_date.getYear() - birth_date.getYear());
var monthDiff = (current_date.getMonth() - birth_date.getMonth());
var daysDiff = (current_date.getDate() - birth_date.getDate());
var hoursDiff = (current_date.getHours() - birth_date.getHours());
var minDiff = (current_date.getMinutes() - birth_date.getMinutes());
var secDiff = (current_date.getSeconds() - birth_date.getSeconds());


     ageCount.innerHTML=YearDiff+' Years '+monthDiff+' Months '+daysDiff+' Days '+hoursDiff+
    ' Hours '+minDiff+' Minutes '+secDiff+' Seconds';

},500);

});

https://jsfiddle.net/DinoMyte/138bhovx/2/

答案 1 :(得分:-1)

您只需使用以下实现进行此计算即可。

https://jsfiddle.net/wqm704xm/1/

var birthday = new Date('Dec,15,1992');
var today;
var $dateEl = document.getElementById('date');

setInterval(writeValue, 100);

function writeValue() {
   today = new Date();
   var calculatedDate = calculate(normalizeDate(birthday), normalizeDate(today));

   $dateEl.innerHTML = 
    'years: ' + calculatedDate.year + '<br>' +
    'months: ' + calculatedDate.month + '<br>' +
    'days: ' + calculatedDate.day + '<br>' +
    'hours: ' + calculatedDate.hours + '<br>' +
    'minutes: ' + calculatedDate.minutes + '<br>' +
    'seconds: ' + calculatedDate.seconds + '<br>' 
  ;
}
  

function calculate(firstDate, currentDate) {
     return {
         year: currentDate.year - firstDate.year,
         month: currentDate.month - firstDate.month,
         day: currentDate.day - firstDate.day,
         hours: currentDate.hours - firstDate.hours,
         minutes: currentDate.minutes - firstDate.minutes,
         seconds: currentDate.seconds - firstDate.seconds
     }
}

function normalizeDate(date) {
  return {
    year: date.getFullYear(),
    month: date.getMonth() + 1,
    day: date.getDate(),
    hours: date.getHours(),
    minutes: date.getMinutes(),
    seconds: date.getSeconds()
  };
}
<div id="date"></div>

答案 2 :(得分:-3)

月份不正确,因为您假设每个月只有30天,而月份可以是28天,29天,30天或31天(在某些情况下,其他长度)。

要快速查看出错的地方,忽略闰年,每月30天/每天将意味着30 * 12,每年只有360天。这意味着每年你错过5天。因此,给定23年,这大约是23 * 5天的差异,或者〜4个月。

在您的约会和现在之间有5个闰年,相隔5天。这完全占你4个月(120天)。

您需要调整算法以解决这些差异。

其他信息:

如果您在生产代码中使用此代码并要求准确的日期 - 请记住,日期不足以计算天数方面的精确差异。您还需要时区和位置等信息,并处理各种可爱的时间中断。

例如:2011年Samoa jumped to the other side of the international date line。因此29-Dec-20111-Jan-2012在澳大利亚的时间比在萨摩亚长1天。

(╯°□°)╯(┻━┻

相关问题