Javascript Calculate Years And Days From Start Date
Solution 1:
I wrote the following a while back, you should be able to tweak it to do the job:
// Given a date object, calcualte the number of// days in the monthfunctiondaysInMonth(x) {
var d = newDate(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.
*/functiongetAge(birthDate, datumDate) {
// Make sure birthDate is before datumDateif (birthDate - datumDate > 0) returnnull;
var dob = newDate(+birthDate),
now = newDate(+datumDate),
tDate = newDate(+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 manyif (now < tDate) {
--years;
--dobY;
}
dob.setYear(dobY);
// Repair tDate
tDate = newDate(+dob);
// Initial month estimate
months = now.getMonth() - tDate.getMonth();
// Adjust if neededif (months < 0) {
months = 12 + months;
} elseif (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 = newDate(+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';
}
functionparseDMY(s) {
var b = s.split(/\D/);
var d = newDate(b[2], --b[1], b[0]);
return d && d.getMonth() == b[1]? d : newDate(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, newDate());
returnfalse;
}
}
<formid="ageCalc"><table><tr><td>Date of Birth (d/m/y)
<td><inputname="dob"><tr><td>Age today (y, m, d)
<td><inputreadonlyname="age"><tr><td><inputtype="reset"><td><inputtype="submit"value="Calculate Age"></table></form>
Here's a straight years and days version:
functiondiffInYearsAndDays(startDate, endDate) {
// Copy and normalise datesvar d0 = newDate(startDate);
d0.setHours(12,0,0,0);
var d1 = newDate(endDate);
d1.setHours(12,0,0,0);
// Make d0 earlier date// Can remember a sign here to make -ve if swappedif (d0 > d1) {
var t = d0;
d0 = d1;
d1 = t;
}
// Initial estimate of yearsvar dY = d1.getFullYear() - d0.getFullYear();
// Modify start date
d0.setYear(d0.getFullYear() + dY);
// Adjust if requiredif (d0 > d1) {
d0.setYear(d0.getFullYear() - 1);
--dY;
}
// Get remaining difference in daysvar dD = (d1 - d0) / 8.64e7;
// If sign required, deal with it herereturn [dY, dD];
}
alert(diffInYearsAndDays(newDate(1957, 11, 4), newDate(2012, 11, 2))); // [54, 364]
Solution 2:
You could roll your own... but I would highly recommend something like Moment.js. It is a solid library that has been proven in the field for calculating Math like this. It is only 4k, so I think you will be fine on size also.
Solution 3:
At end solution was not related to any libraty but logic. I set start age hours and minutes to same as current day hour and minute. Then I keep adding 12 months until year is over current. That was for counting full years. I then set last "birthday" that is not over current date to be info to take and use standard date difference in days against that date and current date.
It worked as charm.
Thanks everyone on great help. It helped me focus on topic.
Post a Comment for "Javascript Calculate Years And Days From Start Date"