Jump to content

_datediff rounding


amfmc
 Share

Recommended Posts

Hello,

I'm rather new to AutoIT but I'm using some code that uses the _datediff function and I was curious if it's possible to not have it round the difference. I've done as much research as I could to possibly find an answer about this but I can't seem to find one so I figured I would ask.

Thanks

Link to comment
Share on other sites

9 minutes ago, amfmc said:

I was curious if it's possible to not have it round the difference

What do you mean?  You want it to not round the difference to what, the nearest minute, hour, day, week, month?  Please provide a better explanation of what you're trying to do.  It would also probably help if you posted the script or a good example of what you are working with and the expected result.

Edited by TheXman
Link to comment
Share on other sites

6 minutes ago, TheXman said:

What do you mean?  Round the difference to what, the nearest minute, hour, day, week, month?  Please provide a better explanation of what you're trying to do.  It would also probably help if you posted the script or a good example of what you are working with.

Sorry, I'm doing a _datediff between two dates to figure out how many hours are in between that date and time.

; Calculates the hours between when the task was created and then worked.
Local $iDateCalc = _DateDiff('h', $createdDate, $workedDate)

After it does this different it comes out to be a rounded number and I don't want that. If "23.2345111" hours passed I want to see that not just "23"

Link to comment
Share on other sites

Why not calculate the difference in minutes and then divide by 60?

#include <Date.au3>

example()

Func example()

    Local $iDiffMins = _DateDiff("n", "2020/10/01 00:00:00", _NowCalc())

    ConsoleWrite('Diff in Mins  = ' & $iDiffMins & @CRLF)
    ConsoleWrite('Diff in Hours = ' & $iDiffMins / 60 & @CRLF)

EndFunc

Output:

Diff in Mins  = 785
Diff in hours = 13.0833333333333

 

Edited by TheXman
Link to comment
Share on other sites

7 minutes ago, TheXman said:

Why not calculate the difference in minutes and then divide by 60?

#include <Date.au3>

example()

Func example()

    Local $iDiffMins  = _DateDiff("n", "2020/10/01 00:00:00", _NowCalc())
    Local $fDiffHours = $iDiffMins / 60
    ConsoleWrite('Diff in Mins  = '  & $iDiffMins & @CRLF)
    ConsoleWrite('Diff in hours = ' & $fDiffHours & @CRLF)

EndFunc

Output:

Diff in Mins  = 785
Diff in hours = 13.0833333333333

 

That's because it never occurred to me. It seems like that will work for me. I will do some testing with it.

Thanks!

Link to comment
Share on other sites

You're welcome!  :thumbsup:

If you want even more precision, then you can calculate the diff in secs and divide secs by 3600 to get hours.

#include <Date.au3>

example()

Func example()

    Local $iDiffSecs = _DateDiff("s", "2020/10/01 00:00:00", _NowCalc())

    ConsoleWrite('Diff in Secs  = ' & $iDiffSecs        & @CRLF)
    ConsoleWrite('Diff in Mins  = ' & $iDiffSecs / 60   & @CRLF)
    ConsoleWrite('Diff in Hours = ' & $iDiffSecs / 3600 & @CRLF)

EndFunc

Console:

Diff in Secs  = 48288
Diff in Mins  = 804.8
Diff in Hours = 13.4133333333333

 

Edited by TheXman
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...