I am trying to record age at the time of taking the survey, using a an embedded data (Date of birth) in the format YYY-MM-dd. Because date of birth is in 1 embedded field, and not 3 (year of birth, month of birth, day of birth) I can't do an age calculation in survey flow (which is fine as the logic for this is a bit messy and I have a lot of surveys I'd need to add this to).
I imagine javascript code could be written that will calculate the age based on the date if birth field I have, but I am not sure how to write it. Can anyone help?
Page 2 / 2
I'd highly suggest integrating Moment.js to handle this. The relative time setup can get you what you are looking for.
And as always! If you want more detailed information, I'm glad to code something up for ya!
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.
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.
Page 2 / 2
Leave a Reply
Enter your username or e-mail address. We'll send you an e-mail with instructions to reset your password.