How to Calculate Age in Years - a simple method
Community Alums
Not applicable
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2024 09:04 AM
Hi,
I've come across many different solutions to this, and most of them assume that calculating the number of days between two dates and dividing the answer by 365 will give someone's age. It won't, as it doesn't take into account leap years where there are 366 days. I've even seen solutions calculating the days and working out the leap years, with mixed results.
If you just care about how old someone/thing is in years, then you can use this simple solution, below. You may need to adjust for local dates, but here goes:
_calculateAge: function(dob) {
// Get DoB
var gd1 = new GlideDate();
gd1.setValue(dob);
var yr1 = gd1.getByFormat("yyyy");
var mon_day1 = gd1.getByFormat("MMdd");
// Get Today
var gd2 = new GlideDate();
var yr2 = gd2.getByFormat("yyyy");
var mon_day2 = gd2.getByFormat("MMdd");
// Calculate age
var age = yr2 - yr1;
if (mon_day2 < mon_day1)
age --;
return age;
},
Please mark as helpful, if it is - many thanks!
Jason
0 REPLIES 0