cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Anonymous
Not applicable

Displaying date difference in both hours and minutes

I have two dates, declared as StartDate and EndDate. I can display the difference between each date in either hours or minutes, but I can not figure out how to display both hours and the remaining minutes. 

 

For example, f the StartDate is the 1st April 2019 at 1:00 am and EndDate is the 2nd of April 2019 at 1:30 am, I want it to calculate the difference as 24 hours and 30 mins rather than rounding to the nearest hour with the DateDiff function.

 

This is the formula in use: DateDiff(txtStartDate.SelectedDate, txtEndDate.SelectedDate, Hours)

 

Any help would be appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Power Apps
Power Apps

Re: Displaying date difference in both hours and minutes

You can use the DateDiff function twice, one for the hours and one for the minutes, and take the value of the minutes modulus 60 (to get only the minutes part), like in the expression below (where I get the start/end times from a date picker and two dropdown controls):

DateDiff(
    dtStart.SelectedDate + Time(Value(ddHourStart.Selected.Value), Value(ddMinuteStart.Selected.Value), 0),
    dtEnd.SelectedDate + Time(Value(ddHourEnd.Selected.Value), Value(ddMinuteEnd.Selected.Value), 0),
    Hours) &
":" &
Mod(
    DateDiff(
        dtStart.SelectedDate + Time(Value(ddHourStart.Selected.Value), Value(ddMinuteStart.Selected.Value), 0),
        dtEnd.SelectedDate + Time(Value(ddHourEnd.Selected.Value), Value(ddMinuteEnd.Selected.Value), 0),
        Minutes),
    60)

The attached app shows this formula in action. To open it, save it locally, then go to https://create.powerapps.com, select Open, Browse, and find the file that you saved before.

Hope this helps!

View solution in original post

10 REPLIES 10
Highlighted
Power Apps
Power Apps

Re: Displaying date difference in both hours and minutes

You can use the DateDiff function twice, one for the hours and one for the minutes, and take the value of the minutes modulus 60 (to get only the minutes part), like in the expression below (where I get the start/end times from a date picker and two dropdown controls):

DateDiff(
    dtStart.SelectedDate + Time(Value(ddHourStart.Selected.Value), Value(ddMinuteStart.Selected.Value), 0),
    dtEnd.SelectedDate + Time(Value(ddHourEnd.Selected.Value), Value(ddMinuteEnd.Selected.Value), 0),
    Hours) &
":" &
Mod(
    DateDiff(
        dtStart.SelectedDate + Time(Value(ddHourStart.Selected.Value), Value(ddMinuteStart.Selected.Value), 0),
        dtEnd.SelectedDate + Time(Value(ddHourEnd.Selected.Value), Value(ddMinuteEnd.Selected.Value), 0),
        Minutes),
    60)

The attached app shows this formula in action. To open it, save it locally, then go to https://create.powerapps.com, select Open, Browse, and find the file that you saved before.

Hope this helps!

View solution in original post

Highlighted
Microsoft
Microsoft

Re: Displaying date difference in both hours and minutes

The Mod function gives you the remainder after integer division.

 

So I think an expression that gives you what you want would be:

RoundDown(
    DateDiff(
        txtStartDate.SelectedDate, 
        txtEndDate.SelectedDate, 
        Minutes
    )/60
) & " hours and " & 
Mod(
    DateDiff(
        txtStartDate.SelectedDate, 
        txtEndDate.SelectedDate, 
        Hours
    ),60
) & "minutes"

 

 

Highlighted
Anonymous
Not applicable

Re: Displaying date difference in both hours and minutes

Thank you very much, this was what I was looking for!

Highlighted
Anonymous
Not applicable

Re: Displaying date difference in both hours and minutes

Hi Carlos.

 

This formula has been working for me until today. The first DateDiff has invalid arguments and the error reads "The DateDIff function has some invalid arguments." and the end of the first formula where Hours is, an error message reads "Invalid arguments type (Number). Expecting a Text value instead."

 

The other DateDIff formula works fine but for some reason, Hours is not working. Minutes, Days and Years works fine.

 

Any idea as to why this is? 

 

Thanks!

Highlighted
Frequent Visitor

Re: Displaying date difference in both hours and minutes

Hello Carlos, it doesn't seem to be working.
the difference between hours:minutes is not calculated when there are minutes; the formula adds an hour in my case; please let me know how to correct this.

HoursMinutes).png

Highlighted
Power Apps
Power Apps

Re: Displaying date difference in both hours and minutes

@Pierrot-56 what is the expression that you are currently using?

Highlighted
Frequent Visitor

Re: Displaying date difference in both hours and minutes

@CarlosFigueira Thanks for the reply
The formula is that of the example that you were kind enough to provide


 

DateDiff(
    dtStart.SelectedDate + Time(Value(ddHourStart.SelectedText.Value); Value(ddMinuteStart.SelectedText.Value); 0);
    dtEnd.SelectedDate + Time(Value(ddHourEnd.SelectedText.Value); Value(ddMinuteEnd.SelectedText.Value); 0);
    Hours) &
":" &
Mod(
    DateDiff(
        dtStart.SelectedDate + Time(Value(ddHourStart.SelectedText.Value); Value(ddMinuteStart.SelectedText.Value); 0);
        dtEnd.SelectedDate + Time(Value(ddHourEnd.SelectedText.Value); Value(ddMinuteEnd.SelectedText.Value); 0);
        Minutes);
    60)​

 


DateDiff.png

Highlighted
Power Apps
Power Apps

Re: Displaying date difference in both hours and minutes

It seems like DateDiff for the hours is operating on the hours directly (9 to 13) or rounding it up, instead of the time difference (3:15). You can fix that by calculating the difference in minutes, and rounding down to the nearest 60, with the expression below:

RoundDown(DateDiff(
    dtStart.SelectedDate + Time(Value(ddHourStart.SelectedText.Value); Value(ddMinuteStart.SelectedText.Value); 0);
    dtEnd.SelectedDate + Time(Value(ddHourEnd.SelectedText.Value); Value(ddMinuteEnd.SelectedText.Value); 0);
    Minutes) / 60; 0) &
":" &
Mod(
    DateDiff(
        dtStart.SelectedDate + Time(Value(ddHourStart.SelectedText.Value); Value(ddMinuteStart.SelectedText.Value); 0);
        dtEnd.SelectedDate + Time(Value(ddHourEnd.SelectedText.Value); Value(ddMinuteEnd.SelectedText.Value); 0);
        Minutes);
    60)

Another option to make this expression above a little more readable is to use the With function (which was introduced after I first replied to this post):

With({
        startTime: dtStart.SelectedDate + Time(Value(ddHourStart.SelectedText.Value); Value(ddMinuteStart.SelectedText.Value); 0);
        endTime: dtEnd.SelectedDate + Time(Value(ddHourEnd.SelectedText.Value); Value(ddMinuteEnd.SelectedText.Value); 0)
    };
    With({
        diffMinutes: DateDiff(startTime; endTime; Minutes)
    };
    RoundDown(diffMinutes / 60; 0) & ":" & Mod(diffMinutes; 45)))

Hope this helps!

Highlighted
Frequent Visitor

Re: Displaying date difference in both hours and minutes

Thanks for your time on this, it's working well now.

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Find your favorite faces from the community presenting at the Power Platform Community Conference!

Watch Now

Experience what’s next for Power Apps

See the latest Power Apps innovations, updates, and demos from the Microsoft Business Applications Launch Event.

Power Platform ISV Studio

Power Platform ISV Studio

ISV Studio is designed to become the go-to Power Platform destination for ISV’s to monitor & manage published applications.

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors
Users online (5,604)