2015-12-28 166 views
0

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

你会告诉我为什么显示这个奇怪的结果?

下面的代码是为Javascript HTML和未来

$(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(){ 
 

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

 

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

 
},500); 
 

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

+0

这些日期的问题保持未来很多秒钟..没有任何人有一个普遍的链接,以支付这些问题呢? – CoderPi

+5

个月总是30天? –

+2

我有一个额外的问题给你。闰年... –

回答

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/

+0

我期待着这样的事情,现在我明白了(y) – hmamun

-3

月不正确,因为你是假设每个月只有30天之久,当个能28,29,30或31天(并且在某些情况下,其他长度)。

要快速查看出错的地方,并忽略闰年,每月30个/天将意味着每年只有360天的30 * 12天。这意味着你每年错过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天。

(╯□°°)╯(┻━┻

+2

@cybermonkey:你的链接副本不回答OP的问题(年与年,月和日),并且在它被链接为重复之前我回答了这个问题 –

+0

你回答了这个~1分钟后*它被标记为重复没有显示'直到你提交,虽然)。 – cybermonkey

+1

@cybermonkey它甚至不是重复,但 – Charlie

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

+0

如果今天的月份或日期<生日的月份或日期,则这不起作用。 https://jsfiddle.net/wqm704xm/2/ – JJJ

+0

我更新了它,它支持小时,分钟和秒钟。并添加了它的间隔 –

+0

它仍然有相同的错误。 https://jsfiddle.net/wqm704xm/3/ – JJJ