cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Dorinda
Responsive Resident
Responsive Resident

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
Pstork1
Dual Super User III
Dual Super User III

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
Responsive Resident
Responsive Resident

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
)
)}
)

View solution in original post

Dorinda
Responsive Resident
Responsive Resident

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
)
}
)

View solution in original post

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.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

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

View solution in original post

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Welcome Super Users.jpg

Super User Season 2

Congratulations, the new Super User Season 2 for 2021 has started!

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Users online (1,988)