web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Calculate between two ...
Power Apps
Answered

Calculate between two dates Hours or Days

(0) ShareShare
ReportReport
Posted on by 1,512

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?

Categories:
  • Pstork1 Profile Picture
    69,661 Most Valuable Professional on at

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

    IF(Radio1.SelectedText.Value = "Hours",DateDiff(StartDateTime,EndDateTime,Hours),DateDiff(StartDateTime,EndDateTime,Days))

     

  • Dorinda Profile Picture
    1,512 on at

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

  • Dorinda Profile Picture
    1,512 on at

    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 Profile Picture
    69,661 Most Valuable Professional on at

    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.

  • Dorinda Profile Picture
    1,512 on at

    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

     

  • CU-18081211-6 Profile Picture
    9,272 Moderator on at

    @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 Profile Picture
    1,512 on at

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

  • CU-18081211-6 Profile Picture
    9,272 Moderator on at

    @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 Profile Picture
    1,512 on at

    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

  • Pstork1 Profile Picture
    69,661 Most Valuable Professional on at

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

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 411 Super User 2026 Season 2

#2
Mohsin Ali Profile Picture

Mohsin Ali 338

#3
WarrenBelz Profile Picture

WarrenBelz 256 Most Valuable Professional

Last 30 days Overall leaderboard