2012-07-19 114 views
0

我有日期,有人出生时,我需要用JavaScript或jQuery左右,自出生日期至今,多少年,几天来计算。JavaScript计算开始日期的年月日

所以结果可能会像20年89天。

我需要和维基百科的功能“年龄”一样的结果,因为它需要闰年的记录,而不是闰年。到目前为止,我得到的代码可行,但在某些情况下会犯一天错误。我的功能是:

function DateDiff(date1, date2){ 
var res=((date1.getTime() - date2.getTime())/1000/60/60/24)-offset_d; 
var full_days=Math.floor(res/365.25); 
var f1=Math.round((res%365.25)) 
return full_days + " years, " +f1 + " days" ; } 

感谢您的帮助,我这样做了好几天。

+0

伙计们请帮助我无法计算从某个日期到现在有多少年和几天。 – user1539246 2012-07-20 00:37:18

+0

365.24更准确,因为它值得。 – ErikE 2012-07-20 08:10:32

+0

谢谢,但我刚刚得到完整的工作代码使用moment.js,现在的问题是它显示每个用户的时区,而不是通用的结果。我如何强制moment.js忽略时区?谢谢! – user1539246 2012-07-20 15:51:15

回答

0

你可以推出自己的...但我会强烈推荐像Moment.js。这是一个坚实的图书馆,已经在现场证明这样计算数学。它只有4k,所以我认为你的体型也会很好。

+0

aaronfrost我检查了Moment.js,我没有任何反对任何基于JS的解决方案,因为我真的不能解决这个问题。[aa = moment([2012,1,29]); var b = moment([2007,12,31]); alert(a.diff(b,'years'));我怎么能得到这些日子? – user1539246 2012-07-19 23:11:20

1

我写了下面的一段时间回来,你应该能够调整它做的工作:

// Given a date object, calcualte the number of 
 
// days in the month 
 
function daysInMonth(x) { 
 
    var d = new Date(x); 
 
    d.setDate(1); 
 
    d.setMonth(d.getMonth() + 1); 
 
    d.setDate(0); 
 
    return d.getDate(); 
 
} 
 

 
/* For person born on birthDate, return their 
 
** age on datumDate. 
 
** 
 
** Don't modify original date objects 
 
** 
 
** tDate is used as adding and subtracting 
 
** years, months and days from dates on 29 February 
 
** can affect the outcome, 
 
** 
 
** e.g. 
 
** 
 
** 2000-02-29 + 1 year => 2001-03-01 
 
** 2001-03-01 - 1 year => 2000-03-01 so not symetric 
 
** 
 
** Note: in some systems, a person born on 29-Feb 
 
** will have an official birthday on 28-Feb, other 
 
** systems will have official birthday on 01-Mar. 
 
*/ 
 
function getAge(birthDate, datumDate) { 
 

 
    // Make sure birthDate is before datumDate 
 
    if (birthDate - datumDate > 0) return null; 
 

 
    var dob = new Date(+birthDate), 
 
     now = new Date(+datumDate), 
 
     tDate = new Date(+dob), 
 
     dobY = dob.getFullYear(), 
 
     nowY = now.getFullYear(), 
 
     years, months, days; 
 

 
    // Initial estimate of years 
 
    years = nowY - dobY; 
 
    dobY = (dobY + years); 
 
    tDate.setYear(dobY); 
 

 
    // Correct if too many 
 
    if (now < tDate) { 
 
    --years; 
 
    --dobY; 
 
    } 
 
    dob.setYear(dobY); 
 
    
 
    // Repair tDate 
 
    tDate = new Date(+dob); 
 

 
    // Initial month estimate 
 
    months = now.getMonth() - tDate.getMonth(); 
 

 
    // Adjust if needed 
 
    if (months < 0) { 
 
    months = 12 + months; 
 

 
    } else if (months == 0 && tDate.getDate() > now.getDate()) { 
 
    months = 11; 
 
    } 
 
    tDate.setMonth(tDate.getMonth() + months); 
 

 
    if (now < tDate) { 
 
    --months; 
 
    dob.setMonth(tDate.getMonth() - 1); 
 
    } 
 

 
    // Repair tDate 
 
    tDate = new Date(+dob); 
 

 
    // Initial day estimate 
 
    days = now.getDate() - tDate.getDate(); 
 

 
    // Adjust if needed 
 
    if (days < 0) { 
 
    days = days + daysInMonth(tDate); 
 
    } 
 
    dob.setDate(dob.getDate() + days); 
 

 
    if (now < dob) { 
 
    --days; 
 
    } 
 

 
    return years + 'y ' + months + 'm ' + days + 'd'; 
 
} 
 

 

 
function parseDMY(s) { 
 
    var b = s.split(/\D/); 
 
    var d = new Date(b[2], --b[1], b[0]); 
 
    return d && d.getMonth() == b[1]? d : new Date(NaN); 
 
} 
 

 
window.onload = function() { 
 
    var form = document.forms['ageCalc']; 
 
    form.onsubmit = function() { 
 
    var dob = parseDMY(form.dob.value); 
 
    form.age.value = isNaN(dob)? 'Invalid date' : getAge(dob, new Date()); 
 
    return false; 
 
    } 
 
}
<form id="ageCalc"> 
 
<table> 
 
    <tr> 
 
    <td>Date of Birth (d/m/y) 
 
    <td><input name="dob"> 
 
    <tr> 
 
    <td>Age today (y, m, d) 
 
    <td><input readonly name="age"> 
 
    <tr> 
 
    <td><input type="reset"> 
 
    <td><input type="submit" value="Calculate Age"> 
 
</table> 
 
</form>

这里是一条直线年,日版:

function diffInYearsAndDays(startDate, endDate) { 

    // Copy and normalise dates 
    var d0 = new Date(startDate); 
    d0.setHours(12,0,0,0); 
    var d1 = new Date(endDate); 
    d1.setHours(12,0,0,0); 

    // Make d0 earlier date 
    // Can remember a sign here to make -ve if swapped 
    if (d0 > d1) { 
    var t = d0; 
    d0 = d1; 
    d1 = t; 
    } 

    // Initial estimate of years 
    var dY = d1.getFullYear() - d0.getFullYear(); 

    // Modify start date 
    d0.setYear(d0.getFullYear() + dY); 

    // Adjust if required 
    if (d0 > d1) { 
    d0.setYear(d0.getFullYear() - 1); 
    --dY; 
    } 

    // Get remaining difference in days 
    var dD = (d1 - d0)/8.64e7; 

    // If sign required, deal with it here 
    return [dY, dD]; 
} 

alert(diffInYearsAndDays(new Date(1957, 11, 4), new Date(2012, 11, 2))); // [54, 364] 
+0

谢谢,但我刚刚得到完整的工作代码使用moment.js,现在的问题是它显示每个用户的时区,而不是通用的结果。我如何强制moment.js忽略时区?谢谢! – user1539246 2012-07-20 15:50:41

+0

我不知道,我不使用moment.js。 Javascript日期对象将时间值内部存储为从UTC时代开始的毫秒数,因此它们本质上是UTC。如果用* setUTCHours *和* setYear *替换* setHours *和* setUTCFullYear *,上面的内容会给出UTC结果。 – RobG 2012-07-22 23:17:59

-1

最终解决方案与任何libraty,但逻辑没有关系。我将开始的年龄和分钟设置为与当前的小时和分钟相同。然后,我继续增加12个月,直到一年结束。这是为了计算整整一年。然后,我设置最近的“生日”,即不超过当前日期的信息,并采用与该日期和当前日期相对的日期的标准日期差异。

它作为魅力。

非常感谢大家的帮助。它帮助我专注于主题。

相关问题