Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.

Calculate age between to date's

Avatar

Level 2

Hallo all,

I want to make a list with all the bithdays of the employees. Is it possible that if I give the current date and the birthday date that javascript calculates the age?

Greetings,

Timo

1 Reply

Avatar

Level 4

Hi Timo, You can easily do this.

Try this code:

/* Example of date arithmetic. */
/* Create a Date object with a definite date. */
var d1 = util.scand("mm/dd/yy", "4/11/76");
/* Create a date object containing the current date. */
var d2 = new Date();
/* Number of seconds difference. */
var diff = (d2.valueOf() - d1.valueOf()) / 1000;
/* Print some interesting stuff to the console. */
console.println("It has been " + diff + " seconds since 4/11/1976");
console.println("It has been " + diff / 60 + " minutes since 4/11/1976");
console.println("It has been " + (diff / 60) / 60 + " hours since 4/11/1976");
console.println("It has been " + ((diff / 60) / 60) / 24 + " days since 4/11/1976");
console.println("It has been " + (((diff / 60) / 60) / 24) / 365 + " years since 4/11/1976");

The output of this script would look something like:
It has been 718329600 seconds since 4/11/1976
It has been 11972160 minutes since 4/11/1976
It has been 199536 hours since 4/11/1976
It has been 8314 days since 4/11/1976
It has been 22.7780821917808 years since 4/11/1976


Hope it helps.

Thanks.

-

Abhinav