# Designing a "How long ago did xxx happen?" question

Hi,

I’m trying to develop a question which allows a user to respond to the
question “How long ago did xx happen?” by either entering the exact date
that it happened or the number of days, weeks, months, and/or years ago
that it happened. I’m having a few difficulties with:

1. The calculation to determine the approximate date using the d, m, w, y
entry. I want the final output to be a date in order to validate it
compared to other dates. Right now I have the following calculation, but
it’s giving me an error: date(today() - /data/test/test_days_ago -
/data/test/test_weeks_ago
7 - /data/test/test_months_ago30.42 -
/data/test/test_years_ago
365.25))*

2. Is there an easy way to still desired date output mentioned above even
if one value is not filled. For example if it was 2 weeks ago the entry
would like like this:
Days: no entry
Weeks: 2
Months: no entry
Years: no entry

3. Ensuring that either one (d, m, w, y) or the other (exact date) is
filled. Currently, I’m giving them the option of putting in the d, w, m , y
ago first. If they know the exact date though, they’re instructed to enter
zeros for all responses. Zeros for all responses would then trigger the
display of the exact date entry question. Then all responses for d, w, m,
y AND the exact date would all be ‘Required’ to be filled. Is there a more
elegant way to do this?

Hope this makes sense – thanks!

Hey Rachael,

This is probably going to be tricky (date math often is) but a few thoughts:

https://confluence.dimagi.com/display/commcarepublic/Calculations+with+Dates+and+Times
thoroughly and keep it handy
• For the first issue you’ll likely need to use the double() function to
convert your date to a days-since value, then convert this back to a date()
• For (2) I’d have an intermediate value that is 0 if the question was
answered and the value otherwise. Standard practice is to take the count()
of the question as your truth statement, so (pseudo code):
• if(count(/data/days_ago) > 0, /data/days_ago, 0)
• For (3) you should be able to use count() again with
days/weeks/months/years since in the validation condition for a question
such that you can’t pass unless one is answered

Cheers,
Will

··· On Sun, Jul 10, 2016 at 12:22 PM, Rachael Sabelli wrote:

Hi,

I’m trying to develop a question which allows a user to respond to the
question “How long ago did xx happen?” by either entering the exact date
that it happened or the number of days, weeks, months, and/or years ago
that it happened. I’m having a few difficulties with:

1. The calculation to determine the approximate date using the d, m, w, y
entry. I want the final output to be a date in order to validate it
compared to other dates. Right now I have the following calculation, but
it’s giving me an error: date(today() - /data/test/test_days_ago -
/data/test/test_weeks_ago
7 - /data/test/test_months_ago30.42 -
/data/test/test_years_ago
365.25))*

2. Is there an easy way to still desired date output mentioned above even
if one value is not filled. For example if it was 2 weeks ago the entry
would like like this:
Days: no entry
Weeks: 2
Months: no entry
Years: no entry

3. Ensuring that either one (d, m, w, y) or the other (exact date) is
filled. Currently, I’m giving them the option of putting in the d, w, m , y
ago first. If they know the exact date though, they’re instructed to enter
zeros for all responses. Zeros for all responses would then trigger the
display of the exact date entry question. Then all responses for d, w, m,
y AND the exact date would all be ‘Required’ to be filled. Is there a more
elegant way to do this?

Hope this makes sense – thanks!

You received this message because you are subscribed to the Google Groups
"commcare-users" group.
To unsubscribe from this group and stop receiving emails from it, send an

Thanks Will, that was really helpful. I think I got it now!

··· On Mon, Jul 11, 2016 at 9:08 PM, William Pride wrote:

Hey Rachael,

This is probably going to be tricky (date math often is) but a few
thoughts:

https://confluence.dimagi.com/display/commcarepublic/Calculations+with+Dates+and+Times
thoroughly and keep it handy
• For the first issue you’ll likely need to use the double() function
to convert your date to a days-since value, then convert this back to a
date()
• For (2) I’d have an intermediate value that is 0 if the question was
answered and the value otherwise. Standard practice is to take the count()
of the question as your truth statement, so (pseudo code):
• if(count(/data/days_ago) > 0, /data/days_ago, 0)
• For (3) you should be able to use count() again with
days/weeks/months/years since in the validation condition for a question
such that you can’t pass unless one is answered

Cheers,
Will

On Sun, Jul 10, 2016 at 12:22 PM, Rachael Sabelli raabauer@gmail.com wrote:

Hi,

I’m trying to develop a question which allows a user to respond to the
question “How long ago did xx happen?” by either entering the exact date
that it happened or the number of days, weeks, months, and/or years ago
that it happened. I’m having a few difficulties with:

1. The calculation to determine the approximate date using the d, m, w, y
entry. I want the final output to be a date in order to validate it
compared to other dates. Right now I have the following calculation, but
it’s giving me an error: date(today() - /data/test/test_days_ago -
/data/test/test_weeks_ago
7 - /data/test/test_months_ago30.42 -
/data/test/test_years_ago
365.25))*

2. Is there an easy way to still desired date output mentioned above even
if one value is not filled. For example if it was 2 weeks ago the entry
would like like this:
Days: no entry
Weeks: 2
Months: no entry
Years: no entry

3. Ensuring that either one (d, m, w, y) or the other (exact date) is
filled. Currently, I’m giving them the option of putting in the d, w, m , y
ago first. If they know the exact date though, they’re instructed to enter
zeros for all responses. Zeros for all responses would then trigger the
display of the exact date entry question. Then all responses for d, w, m,
y AND the exact date would all be ‘Required’ to be filled. Is there a more
elegant way to do this?

Hope this makes sense – thanks!

You received this message because you are subscribed to the Google Groups
"commcare-users" group.
To unsubscribe from this group and stop receiving emails from it, send an

You received this message because you are subscribed to a topic in the
To unsubscribe from this topic, visit
To unsubscribe from this group and all its topics, send an email to