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

DateDiff and Hours

I have a datediff thats not working as I expected, probably just something I've done wrong so sharing here for input

 

Let's say the difference between the two times is 4 hrs 55 minutes, instead of returning 4 hours (which i assumed the rounddown would do, my formula is returning 5 hours. Who here knows what I am doing wrong?

 

RoundDown(DateDiff(ThisItem.CLOCKIN,theclock,Hours),0)

_____________________________________________________________________________________
Like my answer? - Hit that Thumbs Up. Resolved the Issue? - Hit Accept as Solution.
This helps others find solutions to future issues!
1 ACCEPTED SOLUTION

Accepted Solutions
wyotim
Resident Rockstar
Resident Rockstar

Hey @TheRobRush, I think that using DateDiff with Hours will return something a bit different as it seems to round the hours to the nearest integer value all on its own and a bit differently than any of the Round functions would. I think it may have to do with the portion of the day that is used or something like that but it doesn't seem to depend solely on the difference in hours.

 

For instance, in some testing using the Now() function, I found a difference of 4.7 hours would return 4 hours but 4.8 would return 5 but later in the day, 4.7 hours returned 5. So instead you might try this:

RoundDown(
    DateDiff(
        ThisItem.CLOCKIN,
        theclock,
        Minutes
    ) / 60,
    0
)

Basically, this converts the difference in minutes into a decimal (non-integer) version of the hours and then rounds that down to the lowest hour. Feel free to let me know if this works out or not.

View solution in original post

5 REPLIES 5
wyotim
Resident Rockstar
Resident Rockstar

Hey @TheRobRush, I think that using DateDiff with Hours will return something a bit different as it seems to round the hours to the nearest integer value all on its own and a bit differently than any of the Round functions would. I think it may have to do with the portion of the day that is used or something like that but it doesn't seem to depend solely on the difference in hours.

 

For instance, in some testing using the Now() function, I found a difference of 4.7 hours would return 4 hours but 4.8 would return 5 but later in the day, 4.7 hours returned 5. So instead you might try this:

RoundDown(
    DateDiff(
        ThisItem.CLOCKIN,
        theclock,
        Minutes
    ) / 60,
    0
)

Basically, this converts the difference in minutes into a decimal (non-integer) version of the hours and then rounds that down to the lowest hour. Feel free to let me know if this works out or not.

View solution in original post

v-yutliu-msft
Community Support
Community Support

Hi @TheRobRush ,

The reason why you get 4 hours instead of 4 hrs 55 minutes is that DateDiff will only return time of one kind of unit.

For example:

DateDiff(....,Hours) will only return how many hours.

DateDiff(....,Minutes) will only return how many minutes.

DateDiff(....,Seconds) will only return how many seconds.

If you want to get result that is combination of hour and minute, you need to transfer.

Try this formula:

Time(DateDiff(ThisItem.CLOCKIN,theclock,Hours),
DateDiff(ThisItem.CLOCKIN,theclock,Minutes)-DateDiff(ThisItem.CLOCKIN,theclock,Hours)*60,
0)

Here's a doc about DateDiff for your reference:

https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-dateadd-datediff

 

 

 

Best regards,

Community Support Team _ Phoebe Liu

Community Support Team _ Phoebe Liu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
TheRobRush
Impactful Individual
Impactful Individual

@wyotim Will try this tomorrow thank you
_____________________________________________________________________________________
Like my answer? - Hit that Thumbs Up. Resolved the Issue? - Hit Accept as Solution.
This helps others find solutions to future issues!

@v-yutliu-msft
Think you misread my question. Not trying to return 4 hrs 55 minutes, trying to round down properly.
_____________________________________________________________________________________
Like my answer? - Hit that Thumbs Up. Resolved the Issue? - Hit Accept as Solution.
This helps others find solutions to future issues!

Hi @TheRobRush ,

Sorry that I did not fully understand your issue.

The reason why you did not get the right answer is that DateDiff function will return approximate number.

Since 4h55min is closer to 5 than 4, so the result will return 5.

I agree with the formula that @wyotim posted.

The most accurate hour result is :
DateDiff(...,....,Minutes)/60

So, I suggest you try this:

RoundDown(DateDiff(...,....,Minutes)/60,0)

 

Best regards,

Community Support Team _ Phoebe Liu

 

Community Support Team _ Phoebe Liu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

secondImage

Demo Extravaganza is Back!

We are excited to announce that Demo Extravaganza for 2021 has started!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Top Solution Authors
Top Kudoed Authors
Users online (26,562)