Date time picker calculations

Hi,

I am wanting to Check In and Check Out in a form.

Using the combined picker I get Check In > 2015-06-25T12:34:00.000+01 and
Check Out > 2015-06-25T12:48:00.000+01.

Is there a simple way of calculating Check Out - Check In?

Much Thanks in Advance

Tim

Tim,

If you use the function double() EG double(2015-06-25T12:34:00.000+01) this
will convert the date to a number that you can then just compare directly
(with more recent times being higher numbers). You should then be able to
take the difference between the dates and then determine the elapsed time.
This is described in more detail here
https://confluence.dimagi.com/display/commcarepublic/Common+Logic+and+Calculations#CommonLogicandCalculations-Calculations
under
the "Common Log with Date/Time" section.

Best,
Will

··· On Thu, Jun 25, 2015 at 8:05 AM, Tim wrote:

Hi,

I am wanting to Check In and Check Out in a form.

Using the combined picker I get Check In > 2015-06-25T12:34:00.000+01 and
Check Out > 2015-06-25T12:48:00.000+01.

Is there a simple way of calculating Check Out - Check In?

Much Thanks in Advance

Tim

--
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
email to commcare-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hi,

I am wanting to Check In and Check Out in a form.

Using the combined picker I get Check In > 2015-06-25T12:34:00.000+01 and
Check Out > 2015-06-25T12:48:00.000+01.

Is there a simple way of calculating Check Out - Check In?

Much Thanks in Advance

Tim

Hi Tim

I will like to do a similar time duration between two time questions in the
same form e.g ( start time and Finish time) i will like to know the
difference in Minutes elapsed. I will also like to store the Duration in
Case property.
Thanks for you help

··· On Thursday, 25 June 2015 13:05:49 UTC+1, Tim wrote:

Hi Will,

I have tried that and it returns no number?

I understand that it works with now() but not with Date/Time picker
Common logic with Date/Time

  • In your form you can use the now() function to get the current date
    and time. This type of data is slightly different from numbers, and other
    common types. You cannot do math or comparisons on this data type directly.
    First, you must convert it to a number (much like you do for dates when you
    want to add or subtract days from a date). A date and time needs to be
    converted to a number before saving it as a case property:
    double(now()). Saving now() directly will only save the date portion
    without the time portion.
    • First convert it to a decimal using the "double()" function. The
      double function will convert the now() value to a number that represents
      the date/ time in days since 1/1/1970. This is similar to doing
      calculations on dates, when you use int() to convert a date value into
      number of days.
    • When the user "checks in" in you can capture the full date and time
      with a hidden value with a calculation: double(now())
    • Save the hidden value to the case. When the user "checks out" you
      can load that value into the "checks out" form (I'll call the hidden value
      'log_in_time').
    • Create another hidden value (I'll call it 'time_worked') represent
      the amount of time that has passed, by calculating: double(now())
    • double(/data/log_in_time)
    • The result is a decimal value in days/fractions of days. To convert
      to hours and minutes, you can do the following calculations:
      • int(/data/time_worked * 24) = (rounded down) the # of hours
      • int(((/data/time_worked*24) - int(/data/time_worked * 24)) * 60)
        = (rounded down) the # of minutes worked.
    • Note that this does not currently work with "time" or "date time"
      question types in the form builder.

Best

Tim

··· On Thursday, 25 June 2015 13:21:24 UTC+1, William Pride wrote: > > Tim, > > If you use the function double() EG double(2015-06-25T12:34:00.000+01) > this will convert the date to a number that you can then just compare > directly (with more recent times being higher numbers). You should then be > able to take the difference between the dates and then determine > the elapsed time. This is described in more detail here > under > the "Common Log with Date/Time" section. > > Best, > Will > > On Thu, Jun 25, 2015 at 8:05 AM, Tim <tim.bu...@gmail.com > wrote: > >> Hi, >> >> I am wanting to Check In and Check Out in a form. >> >> Using the combined picker I get Check In > 2015-06-25T12:34:00.000+01 and >> Check Out > 2015-06-25T12:48:00.000+01. >> >> Is there a simple way of calculating Check Out - Check In? >> >> Much Thanks in Advance >> >> Tim >> >> >> >> >> -- >> 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 >> email to commcare-user...@googlegroups.com . >> For more options, visit https://groups.google.com/d/optout. >> > >

Interesting, sorry about that.

Does using the date() function successfully convert these strings to dates?
If not, the only other strategy I can think of would be manually pulling
out the hours/minutes/seconds values and calculating the difference
manually with a hidden value... though that could get quite ugly. Wonder if
any implementers have good workarounds for this?

Thanks,
Will

··· On Thu, Jun 25, 2015 at 9:38 AM, Tim wrote:

Hi Will,

I have tried that and it returns no number?

I understand that it works with now() but not with Date/Time picker
Common logic with Date/Time

  • In your form you can use the now() function to get the current date
    and time. This type of data is slightly different from numbers, and other
    common types. You cannot do math or comparisons on this data type directly.
    First, you must convert it to a number (much like you do for dates when you
    want to add or subtract days from a date). A date and time needs to be
    converted to a number before saving it as a case property:
    double(now()). Saving now() directly will only save the date portion
    without the time portion.
    • First convert it to a decimal using the "double()" function. The
      double function will convert the now() value to a number that represents
      the date/ time in days since 1/1/1970. This is similar to doing
      calculations on dates, when you use int() to convert a date value into
      number of days.
    • When the user "checks in" in you can capture the full date and
      time with a hidden value with a calculation: double(now())
    • Save the hidden value to the case. When the user "checks out" you
      can load that value into the "checks out" form (I'll call the hidden value
      'log_in_time').
    • Create another hidden value (I'll call it 'time_worked')
      represent the amount of time that has passed, by calculating: double(now())
    • double(/data/log_in_time)
    • The result is a decimal value in days/fractions of days. To
      convert to hours and minutes, you can do the following calculations:
      • int(/data/time_worked * 24) = (rounded down) the # of hours
      • int(((/data/time_worked*24) - int(/data/time_worked * 24)) *
      1. = (rounded down) the # of minutes worked.
    • Note that this does not currently work with "time" or "date
      time" question types in the form builder.

Best

Tim

On Thursday, 25 June 2015 13:21:24 UTC+1, William Pride wrote:

Tim,

If you use the function double() EG double(2015-06-25T12:34:00.000+01)
this will convert the date to a number that you can then just compare
directly (with more recent times being higher numbers). You should then be
able to take the difference between the dates and then determine
the elapsed time. This is described in more detail here
https://confluence.dimagi.com/display/commcarepublic/Common+Logic+and+Calculations#CommonLogicandCalculations-Calculations under
the "Common Log with Date/Time" section.

Best,
Will

On Thu, Jun 25, 2015 at 8:05 AM, Tim tim.bu...@gmail.com wrote:

Hi,

I am wanting to Check In and Check Out in a form.

Using the combined picker I get Check In > 2015-06-25T12:34:00.000+01
and Check Out > 2015-06-25T12:48:00.000+01.

Is there a simple way of calculating Check Out - Check In?

Much Thanks in Advance

Tim

--
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 email to commcare-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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
email to commcare-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hi Will

After more trial and error I finally got there!

Date and Time Picker

double(date(2015-06-25T12:34:00.000+01))

gives full day number plus . for hours

16611.52361111111

:slight_smile:

It has taken a few hours of trial and error and hints to get here, might be
worth adding to the docs :slight_smile:

Thanks for the inspiration.

Best

Tim

Great to hear! Thanks for sharing your code, I'll see that we get them on
the wiki.

-Will

··· On Thu, Jun 25, 2015 at 10:24 AM, Tim wrote:

Hi Will

After more trial and error I finally got there!

Date and Time Picker

double(date(2015-06-25T12:34:00.000+01))

gives full day number plus . for hours

16611.52361111111

:slight_smile:

It has taken a few hours of trial and error and hints to get here, might
be worth adding to the docs :slight_smile:

Thanks for the inspiration.

Best

Tim

--
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
email to commcare-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hi William
having similar issue calculating elapsed time in minutes in same for for
START and END Time/Date and Time question type?

Need help

··· On Thursday, 25 June 2015 15:31:31 UTC+1, William Pride wrote: > > Great to hear! Thanks for sharing your code, I'll see that we get them on > the wiki. > > -Will > > On Thu, Jun 25, 2015 at 10:24 AM, Tim <tim.bu...@gmail.com > wrote: > >> Hi Will >> >> After more trial and error I finally got there! >> >> Date and Time Picker >> >> double(date(2015-06-25T12:34:00.000+01)) >> >> gives full day number plus . for hours >> >> 16611.52361111111 >> >> :) >> >> It has taken a few hours of trial and error and hints to get here, might >> be worth adding to the docs :) >> >> Thanks for the inspiration. >> >> Best >> >> Tim >> >> -- >> 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 >> email to commcare-user...@googlegroups.com . >> For more options, visit https://groups.google.com/d/optout. >> > >