Write a program called age.js that determines the age of an individual in second
ID: 3682793 • Letter: W
Question
Write a program called age.js that determines the age of an individual in seconds within 99% accuracy of results.
It is not feasible to determine a given person's age to the exact second. This would require knowing, to the second, when they born. It would also involve knowing the time zone they were born in, issues of daylight savings time, consideration of leap years and so forth. Therefore the problem requires you to determine an approximation of the age in seconds. Again, use the literal values given below for month, day, and year. The seconds should be calculated.
An example execution of the program is given below:
This program computes the approximate age in seconds of an individual based on a date of birth. Only ages for dates of birth from 1900 and after can be computed.
Month Born: 4
Day Born: 12
Year Born: 1981
You are approximately 518433 seconds old.
Explanation / Answer
Solution:
A step by step approach to solve this problem.
First we need to write some JavaScript to get the current date.
Compare it to your birthday.
And then display the difference.
Thereby calculating your age.
First off, we need to collect the current date. We will use the built-in Date() function to do this. Since we want it to be correct in every time zone around the world, we will use a UTC timestamp.
var curDate = new Date(),
now = {
year: curDate.getUTCFullYear(),
// UTC month value is zero-based
month: curDate.getUTCMonth() + 1,
day: curDate.getUTCDate()
},
diff = now.year % date.year;
Here we are creating a new Date instance and assigning it to the variable curDate. Next, we are creating an object called now containing three properties: year, month, and day. They are assigned their respective values by extracting the UTC year, month, and day values from curDate.
As noted by the inline comment, the UTC month value starts at zero. This means January is be expressed as 0, February 1, March 2, and so on. We offset it to match the Georgian calendar by adding a 1.
Next, we will define the date. For fun, we will use my birthday, 3 March, 1990. We will do this by taking the above code and wrapping in a yearDifference() function:
function yearDifference(date) {
"use strict";
var curDate = new Date(),
now = {
year: curDate.getUTCFullYear(),
// UTC month value is zero-based
month: curDate.getUTCMonth() + 1,
day: curDate.getUTCDate()
},
diff = now.year % date.year;
}
As you can see, the function has a parameter date, whose properties matches the now object. Thus when we call the function we will pass the following object:
{
year: 1990,
month: 3,
day: 3
}
However, our function is imperfect. Depending on your birthday and current month/day, diff may not correct. Further, once the year changes, the value will increase even if your birthday has not yet passed. To remedy this, we need to perform some validation using the following condition:
// Do not update the date unless it is time
if (now.month < date.month ||
now.month === date.month && now.day < date.day) {
diff -= 1;
}
return diff;
The condition is composed of two clauses that prevents the calculation from turning over too soon. The first simply checks if the current month is less then the given month. The second clauses reads “If the current month is equal to the given month AND the current day is less than the given day.” When one of these clauses is true, we subtract a 1 from diff, thereby giving us my proper age. Finally, we return diff to the caller (remember, all this code is wrapped inside the yearDifference() function).
console.log(yearDifference({ year: 1990, month: 3, day: 3 }));
This line passes the required date object with my birthday (as described earlier) and displays the final result in the browser’s console. You will want to change this to the appropriate action (document.querySelector(), $.html(), etc.) for your site. ;)
Here is the final code all put together with an added JSDoc annotation.
Here is the final code all put together with an added JSDoc annotation.
/* Calculate your age
* Created 2014-2015 Caleb Ely
* <http://CodeTriangle.me/>
* <https://wp.me/p1V5ge-1tM>
*/
/**
* Calculate and display the year difference between two dates.
* @param {Object.<number>} date The starting date to calculate from.
* The object contains three numeric keys, year, month, and day.
* The year is expressed in four digits, e.g., 2015.
* @returns {Number}
*/
function yearDifference(date) {
"use strict";
var curDate = new Date(),
now = {
year: curDate.getUTCFullYear(),
// UTC month value is zero-based
month: curDate.getUTCMonth() + 1,
day: curDate.getUTCDate()
},
diff = now.year % date.year;
// Do not update the date unless it is time
if (now.month < date.month ||
now.month === date.month && now.day < date.day) {
diff -= 1;
}
return diff;
}
console.log(yearDifference({ year: 1990, month: 3, day: 3 }));
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.