cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Dorinda
Impactful Individual
Impactful Individual

Calculate between two dates Hours or Days

I have this power app that has a start and stop date and time

Dorinda_0-1612451544758.png

I need to be able to have the work duration field calculate the amount based on which button is checked in the Duration Type

 

So if it is hours how many hours between Start/Stop or for days how many days.

 

Is this possible?

13 REPLIES 13

You don't really need to use context variables, although it does make the code easier to read.  But as I said you do need to create a full date and time value to use in datediff.  But this shoudl work without using the onChange event and context variables.

IF(Radio1.SelectedText.Value = "Hours",DateDiff(
DateTimeValue(
             Text(
              StartDatePicker.SelectedDate,"[$-en-GB]yyyy.mm.dd ")&
             StartHourDropdown.Selected.Value&
             ":"&
             StartMinuteDropdown.Selected.Value
             )),
DateTimeValue(
             Text(
              EndDatePicker.SelectedDate,"[$-en-GB]yyyy.mm.dd ")&
             EndHourDropdown.Selected.Value&
             ":"&
             EndMinuteDropdown.Selected.Value
             )),
             Hours),DateDiff(
DateTimeValue(
             Text(
              StartDatePicker.SelectedDate,"[$-en-GB]yyyy.mm.dd ")&
             StartHourDropdown.Selected.Value&
             ":"&
             StartMinuteDropdown.Selected.Value
             )),
DateTimeValue(
             Text(
              EndDatePicker.SelectedDate,"[$-en-GB]yyyy.mm.dd ")&
             EndHourDropdown.Selected.Value&
             ":"&
             EndMinuteDropdown.Selected.Value
             )),
             Days))


-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
Dorinda
Impactful Individual
Impactful Individual

This is with the correct names, however it doesnt like the part in red in the forumla

UpdateContext({
chosenStartTime:
DateTimeValue(
Text(
DateValue1.SelectedDate,"[$-en-US]yyyy.mm.dd ")&
HourValue1.Selected.Value&
":"&
MinuteValue1.Selected.Value
)
)}
);
UpdateContext({
chosenEndTime:
DateTimeValue(
Text(
DateValue2.SelectedDate,"[$-en-US]yyyy.mm.dd ")&
HourValue2.Selected.Value&
":"&
MinuteValue2.Selected.Value
)
)}
)

Dorinda
Impactful Individual
Impactful Individual

I took off the extra ) and it works perfect!!  Thank you

 

UpdateContext({
chosenStartTime:
DateTimeValue(
Text(
DateValue1.SelectedDate,"[$-en-US]yyyy.mm.dd ")&
HourValue1.Selected.Value&
":"&
MinuteValue1.Selected.Value
)
}
);
UpdateContext({
chosenEndTime:
DateTimeValue(
Text(
DateValue2.SelectedDate,"[$-en-US]yyyy.mm.dd ")&
HourValue2.Selected.Value&
":"&
MinuteValue2.Selected.Value
)
}
)

RandyHayes
Super User
Super User

@Dorinda 

You will have issues with the solution you are trying.

This is quite simple to achieve without the OnChange actions (which will not occur on a form edit - and thus your variable will be empty).

Your Default formula on the WorkDuration datacardvalue control should be the following:

 

 

DateDiff(
      DateTimeValue(
          Text(DateValue1.SelectedDate, ShortDate) & " " & 
               HourValue.Selected.Value & ":" & MinuteValue1.Selected.Value & ":00"
      ),
      DateTimeValue(
          Text(DateValue2.SelectedDate, ShortDate) & " " & 
               HourValue2.Selected.Value & ":" & MinuteValue2.Selected.Value & ":00"
      ),
      Hours
) / If(Radio3.Selected.Value="Hours", 1, 24)

 

 

 

- Get rid of the OnChange actions on the controls.

- Avoid using .SelectedText on dropdowns as it is a Deprecated property.

- Make sure that you have not changed the Default properties of the datepickers or hour or minute controls.  They should be based on the Parent.Default value.

 

EDIT: There was an extra paren in the original formula. It is corrected above.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Helpful resources

Announcements
PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

May UG Leader Call Carousel 768x460.png

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

Top Solution Authors
Top Kudoed Authors
Users online (1,672)