Skip to main content
December 16, 2016
Question

Age in Years & Months

  • December 16, 2016
  • 0 replies
  • 193 views

I am trying to calculate age in years and months using a DOB field and a DOB filed formatted as dd/mm/yyyy

This is the code that I am using and nothing is coming up.  Could someone please help me, I went through old post to get this code and still can't figure out the correct script.  I would appreciate it so much. Thank you.

Age field based on date field

event.value = "";

// get dob string value

var sDob = this.getField("DOB").value;

if(sDob != "") {

// convert to date object

var oDob = util.scand("dd-mmm-yyyy", sDob);

// todays date in milliseconds since Epoch date

var oToday = new Date()

// compute output

nYears = oToday.getFullYear() - oDob.getFullYear();

nMonths = oToday.getMonth() - oDob.getMonth();

nDays = oToday.getDate() - oDob.getDate();

if(nDays < 0) {

// table for months days

// fix for days in month

nMonths--;

nDays = nDays + 30;

}

if(nMonths < 0) {

nYears--;

nMonths = nMonths + 12;

}

event.value = nYears + " years " + nMonths + "  months " + nDays + " days";

}

This topic has been closed for replies.