JavaScript to breakup date of birth for doing an age calculation | XM Community

JavaScript to breakup date of birth for doing an age calculation



Show first post

28 replies

Userlevel 7
Badge +7
I'd highly suggest integrating Moment.js to handle this. The relative time setup can get you what you are looking for.
Userlevel 4
Badge +3
And as always! If you want more detailed information, I'm glad to code something up for ya!
Userlevel 4
Badge +3
Yeah! With Javascripts `Date()` object, you can do a few different things.
I would honestly reach out for a library that could calculate the dates: Moment.JS is the best for that, in my opinion.

Here is a link on how to include it in your Qualtrics project via CDN!
http://momentjs.com/docs/

It allows you to get the absolute difference between two dates as documented here: http://momentjs.com/docs/#/displaying/difference/

Then, you can get the duration between their birthdate and the current date, and break that into years, months, days, and even seconds if you're adventurous.

If the library is too large, or you want to do it Vanilla, there are a few functions out there that others have coded that can get you the absolute difference between a birthday and the current date, but it gets tricky.

Leave a Reply