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?

3 ACCEPTED SOLUTIONS

Accepted Solutions
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

13 REPLIES 13
Pstork1
Dual Super User III
Dual Super User III

Just use an IF() statement and two DateDiff() functions

IF(Radio1.SelectedText.Value = "Hours",DateDiff(StartDateTime,EndDateTime,Hours),DateDiff(StartDateTime,EndDateTime,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

Do i use the Datacard or the actual field?  This is on NewForm, if that makes a difference

Dorinda
Responsive Resident
Responsive Resident

Because both are still giving me errors, this doesnt work and it doesnt work with DateValue1 and DateValue2

If(Radio3.SelectedText.Value = "Hours",DateDiff(WorkStartTime,WorkCompleteTime,Hours),DateDiff(WorkStartTime,WorkCompleteTime,Days))

Pstork1
Dual Super User III
Dual Super User III

You need to take the values from the datacards for the date and time and put them together to get a DateTime value for the start time and the end time.  You can't do it just on time.



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

Tried this got no number

 

If(Radio3.SelectedText.Value = "Hours",DateDiff(WorkStartTime_DataCard1.Default,WorkCompleteTime_DataCard1.Default,Hours),DateDiff(WorkStartTime_DataCard1.Default,WorkCompleteTime_DataCard1.Default,Days))

 

Dorinda_0-1612453259898.png

 

@Dorinda ,

 

To get a correct answer, a least when we are talking about hours, you need to use 2 context variables to keep the datetimeformat type for start and for end.

1. For The three controls represents the start datetime and the three controls represents the end datetime:

Set OnChange property to : 

 

UpdateContext({
    chosenStartTime:
    DateTimeValue(
             Text(
              StartDatePicker.SelectedDate,"[$-en-GB]yyyy.mm.dd ")&
             StartHourDropdown.Selected.Value&
             ":"&
             StartMinuteDropdown.Selected.Value
             )
    )}
);
UpdateContext({
    chosenEndTime:
    DateTimeValue(
             Text(
              EndDatePicker.SelectedDate,"[$-en-GB]yyyy.mm.dd ")&
             EndHourDropdown.Selected.Value&
             ":"&
             EndMinuteDropdown.Selected.Value
             )
    )}
)

 

2. The workduration control (most propably a textinput control)

Default:

 

DateDiff(
      chosenStartTime,
      chosenEndTime,
      if( Radio3.SelectedText.Value = "Hours",Hours,Days)
)

 

Dorinda
Responsive Resident
Responsive Resident

which onchange do i put this on start or end or both?

@Dorinda ,

In all six controls, that it will assure us that we have the selected startdate time and enddate time as showed by controls.

Please identify the correct name of the controls (in formula) as I put a general name to help you easily identify the controls. 

Also, please keep in mind that the two variables will load with values only after one of six controls will be changed.

To have the value loaded from beginning use the OnVisible property of the screen with the above formula.

Dorinda
Responsive Resident
Responsive Resident

does this matter that it is in a form, and that it wants thisitem. something or will it matter because i tried what you gave me and it errored

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!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Solution Authors
Top Kudoed Authors
Users online (2,035)