Jump to content

Simple Date and Time compare issue


Kovacic
 Share

Go to solution Solved by BrewManNH,

Recommended Posts

I know this should be simple, and I dont know why I am having so much trouble with it, but what I am trying to do is compare the current time and date vs. a future time and date. The format of both dates must be: YYYY/MM/DD hh:mm:ss

This is what I have so far as a test:

#include "date.au3"
$FutureTime = "2014/11/25 15:12:05"
$days    = _DateDiff("D",@year & "/" & @mon & "/" & @MDAY & " " & @hour & ":" & @min & ":" & @sec ,$FutureTime)
$hours   = _DateDiff("h",@year & "/" & @mon & "/" & @MDAY & " " & @hour & ":" & @min & ":" & @sec ,$FutureTime)
$minutes = _DateDiff("m",@year & "/" & @mon & "/" & @MDAY & " " & @hour & ":" & @min & ":" & @sec ,$FutureTime)
$seconds = _DateDiff("s",@year & "/" & @mon & "/" & @MDAY & " " & @hour & ":" & @min & ":" & @sec ,$FutureTime)
MsgBox(0,"Time until..",$days & " Days, " & $hours   & " Hours, " & $minutes  & " minutes, " & $seconds & " seconds")

I do understand why this wont work, because for each of hours, minutes, seconds, etc, it is giving the total time between the dates in each variable as per the _DateDiff knowledgeable article. I am hoping to get something more like:  20 Days, 4 Hours, 6 minutes and 25 seconds...

Does anyone already have a small chunk of code I can use, or at least be able to shed some light?

Thanks in advance!

C0d3 is P0etry( ͡° ͜ʖ ͡°)

Link to comment
Share on other sites

Kovacic,

One way...

#include <date.au3>

_duration('2015/11/05 12:00:00')

func _duration($target_date)

    $diff = _datediff('s',_NowCalc(),$target_date)

    ; calcs adapted from code by UEZ

    local $days
    Local $secs = Mod($diff, 60)
    Local $mins = Mod(Int($diff / 60), 60)
    Local $hrs  = Int($diff / 60 ^ 2)
    If $hrs > 23 Then
        $days = Floor($hrs/ 24)
        $hrs -= $days * 24
    endif

    local $diff_out = stringformat('%02i Days %02i Hours %02i Minutes %02i Seconds to ' & stringleft($target_date,stringinstr($target_date,' ')), $days, $hrs, $mins, $secs)
    ConsoleWrite($diff_out & @CRLF)

endfunc

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

  • Solution

Another way using the math needed for the conversion.

#include "date.au3"
$FutureTime = "2014/11/25 18:12:05"
$days = _DateDiff("D", @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC, $FutureTime)
$hours = _DateDiff("h", @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC, $FutureTime)
$minutes = _DateDiff("m", @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC, $FutureTime)
$seconds = _DateDiff("s", @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC, $FutureTime)
$iDays = Int($seconds / 86400)
$iHours = Int(($seconds - ($iDays * 86400)) / 3600)
$iMinutes = Int((($seconds - ($iDays * 86400)) - ($iHours * 3600)) / 60)
$iSeconds = Int(((($seconds - ($iDays * 86400)) - ($iHours * 3600) - ($iMinutes * 60)) * 60) / 60)
MsgBox(0, "Time until..", $iDays & " Days, " & $iHours & " Hours, " & $iMinutes & " minutes, " & $iSeconds & " seconds")

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

 

Another way using the math needed for the conversion.

#include "date.au3"
$FutureTime = "2014/11/25 18:12:05"
$days = _DateDiff("D", @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC, $FutureTime)
$hours = _DateDiff("h", @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC, $FutureTime)
$minutes = _DateDiff("m", @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC, $FutureTime)
$seconds = _DateDiff("s", @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC, $FutureTime)
$iDays = Int($seconds / 86400)
$iHours = Int(($seconds - ($iDays * 86400)) / 3600)
$iMinutes = Int((($seconds - ($iDays * 86400)) - ($iHours * 3600)) / 60)
$iSeconds = Int(((($seconds - ($iDays * 86400)) - ($iHours * 3600) - ($iMinutes * 60)) * 60) / 60)
MsgBox(0, "Time until..", $iDays & " Days, " & $iHours & " Hours, " & $iMinutes & " minutes, " & $iSeconds & " seconds")

 

You are an awesome human being! don't ever let anyone tell you otherwise!

problem solved!

C0d3 is P0etry( ͡° ͜ʖ ͡°)

Link to comment
Share on other sites

I really need to save a snippet like this, getting the math to work out correctly is not my strong suit. :D
 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I really need to save a snippet like this, getting the math to work out correctly is not my strong suit. :D

 

 

Thats where I fell short... I knew that an extensive amount of math had to be done, but I also knew it would take me many hours and head aches to figure it out. That would be cool, a global database of snippits.. I know the KB has them with each function, but math functions like the one above are priceless and wont be found in the KB.

C0d3 is P0etry( ͡° ͜ʖ ͡°)

Link to comment
Share on other sites

BTW, just so anyone looking at this in the future, you can eliminate the lines for $days, $minutes, $hours as they're not needed. I just forgot to delete them.

Also, this snippet will work for dates in the past as well as the future.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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