cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Owizardo
Level: Powered On

DateDiff in hours not accurate

Hello,

 

Found a weird problem, it seems like the DateDiff is not always accurate. For example look at this formula:

 

DateDiff(
    DateTimeValue("2019-05-23 10:25"),
    DateTimeValue("2019-05-23 16:00"), Hours
)

I would expect 5 hours but it gives 6 hours.

 

Example of accurate calculator:

https://www.timeanddate.com/date/durationresult.html?m1=5&d1=24&y1=2019&m2=5&d2=24&y2=2019&h1=10&i1=...

 

Anyone know if theres anything I can do about this? Meantime im gonna have to recode my application to use minutes Smiley Sad.

 

1 ACCEPTED SOLUTION

Accepted Solutions
BrianR
Level 10

Re: DateDiff in hours not accurate

Does seem odd, initially I thought that it was just rounding, so changing your 10:25 to something like 10:35 would result in 5 instead, though this appears to NOT be the case (both return 6).  

But what you can do, as suggested, it to use Minutes:

DateDiff(
    DateTimeValue("2019-05-23 10:25"),
    DateTimeValue("2019-05-23 16:00"), Minutes
) / 60

which will return 5.58333333 in this case.  Or if you want just the "5":

RoundDown( DateDiff(
    DateTimeValue("2019-05-23 10:25"),
    DateTimeValue("2019-05-23 16:00"), Minutes
) / 60, 0 )


I do tend to agree though that DateDiff() in your example seems to be returning the WRONG answer - either have it return the full fractional "answer" (including taking minutes and seconds into account), or else round that down to have 5.  

But looks like its ONLY looking at the Hours part, and ignoring minutes and seconds, so my workaround SHOULD work (or to be more accurate use Seconds and / 3600).  :-)

4 REPLIES 4
BrianR
Level 10

Re: DateDiff in hours not accurate

Does seem odd, initially I thought that it was just rounding, so changing your 10:25 to something like 10:35 would result in 5 instead, though this appears to NOT be the case (both return 6).  

But what you can do, as suggested, it to use Minutes:

DateDiff(
    DateTimeValue("2019-05-23 10:25"),
    DateTimeValue("2019-05-23 16:00"), Minutes
) / 60

which will return 5.58333333 in this case.  Or if you want just the "5":

RoundDown( DateDiff(
    DateTimeValue("2019-05-23 10:25"),
    DateTimeValue("2019-05-23 16:00"), Minutes
) / 60, 0 )


I do tend to agree though that DateDiff() in your example seems to be returning the WRONG answer - either have it return the full fractional "answer" (including taking minutes and seconds into account), or else round that down to have 5.  

But looks like its ONLY looking at the Hours part, and ignoring minutes and seconds, so my workaround SHOULD work (or to be more accurate use Seconds and / 3600).  :-)

Super User
Super User

Re: DateDiff in hours not accurate

How about using this:

DateDiff(DateTimeValue(Time1), DateTimeValue(Time2)) * 24 + Hour(DateTimeValue(Time1) -   Hour(DateTimeValue(Time2))

Owizardo
Level: Powered On

Re: DateDiff in hours not accurate

Not quite sure I understand this formula but it it returns me 10 hours...

Super User
Super User

Re: DateDiff in hours not accurate

@BrianR's Rounddown formula should get you what you want.

Helpful resources

Announcements
firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

Top Community Contributors for July 2019

Top Community Contributors for July 2019

Let's thank our top community contributors

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

FirstImage

Power Platform World Tour

Coming to a city near you

thirdimage

PowerApps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

FourthImage

Join PowerApps User Group!!

Connect, share, and learn with your peers year-round

FifthImage

Dynamics 365 and Power Platform April 2019 Release notes

Features releasing from April 2019 through September 2019!

Users Online
Currently online: 4 members 5,119 guests
Recent signins:
Please welcome our newest community members: