How Old Are My Google Contacts?

It may be a sign of old age but, I’m struggling more and more to remember how old people are. I have addressed this by having a review of my Google contact data and making sure, where I know someone’s birthday I have recorded it there.

This has the added advantage of creating a Google calendar showing me their birthdays.

I have now created a script which scans my contacts to find their birthdays, does some arithmetic to work out their age and store this in a custom field. I have set up a trigger to run this once a week.

Here is the script that I created:

function updateAges() {
var group = ContactsApp.getContactGroup(‘System Group: My Contacts’);
var contacts = group.getContacts();
for (var aContactCount in contacts) {
var aContact = contacts[aContactCount];
var arrAge = aContact.getCustomFields(‘Age’);
for (var ageCount in arrAge) {
var aAge = arrAge[ageCount];
var aBirthday = aContact.getDates(ContactsApp.Field.BIRTHDAY);
if (aBirthday.length > 0) {
var strDOB = aBirthday[0].getYear() + ‘/’ + aBirthday[0].getMonth() + ‘/’ + aBirthday[0].getDay();
var intAge = Math.floor(( – Date.parse(strDOB)) / (24 * 3600 * 365.25 * 1000));
aContact.addCustomField(‘Age’, intAge)
Logger.log(‘Updated ‘ + aContact.getFullName() + ‘ to add the age ‘ + intAge)

You can find the code on GitHub here.

The script is nice and simple:

  • For a given group, find all of the contacts.
  • If the contact has a custom field called “Age”, delete it.
  • If the contact has a birthday recorded, work out their age.
  • Store the age in a custom field called “Age”.

Usual disclaimers apply – I’m not a coder so this may not be the “best” or most efficient code – but it works for me.

I “know” there are problems with the accuracy of the date arithmetic but it’s close enough for what I need.

Leave a Reply

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Learn More)