The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Date validation help

Are Kaveri
Tera Contributor
Hi I have below requirement, We have a date field, for which i need to calculate duration of the date. Date of birth = 21-09-1994 I need to calculate age of the person. Please help me with calculation.
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Are Kaveri 

this link has solution

Autofill of Age based on Date of Birth 

few more link

Calculate Age from DoB in Client Script 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Are Kaveri 

this link has solution

Autofill of Age based on Date of Birth 

few more link

Calculate Age from DoB in Client Script 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Joe Wong
Tera Guru

Hi Are,

 

In order to calculate a accurate birthday to the date, you need to take into account of the current date of the year too.  I see the scripts that has been link to only takes into account the year but not whether if the birthdate has occurred yet, if that matters to your app.  Here is the programmer in me going wild... this is quite a long script that takes into account the date of birth.

 

There are definitely other solution to your problem, but this is my first heck at it.  Someone might be able to streamline this script better.  This script will calculate your age to the date, so if you enter Jan 16, 1994 vs Jan 17, 1994, you will get a different answer.

 

****************************************************************************

var bDate = new GlideDateTime();
bDate.setValue("01-17-1994");
bDate = String(bDate); // turn birthdate into string

var curDate = new GlideDateTime();
curDate = String(curDate); // turn current date into string

// extract year from birthdate and current date and covert them into integers
var bdYear = parseInt(bDate.substring(0,4));
var curYear = parseInt(curDate.substring(0,4));

// calculate the difference
age = curYear - bdYear;

// extract month from birthdate and current date and covert them into integers
// dev note: the second arguement is needed in parseInt for force leading '0' to be evaluated as base 10 instead of base 8
var bdMonth = parseInt(bDate.substring(5,7), 10);
var curMonth = parseInt(curDate.substring(5,7), 10);

if(bdMonth > curMonth) {
    // if birth month is greater than current month, current year birthdate has not occur yet, subtract 1 year from the age calculated.
    age = age - 1;
} else if(bdMonth == curMonth) {
    // if birth month is the same as current month, compare dates

    // extract date from birthdate and current date and covert them into integers
    // dev note: the second arguement is needed in parseInt for force leading '0' to be evaluated as base 10 instead of base 8
    var bdDay = parseInt(bDate.substring(8,10), 10);
    var curDay = parseInt(curDate.substring(8,10), 10);
    if(bdDay > curDay) {
        // if birth date is greater than current date, current year birthdate has not occur yet, subtract 1 year from the age calculated.
        age = age - 1;
    }
}
gs.print(age);
****************************************************************************