Jump to content

How to Add hours to _nowtime


Recommended Posts

I have searched the forums and google for awhile now but can not locate what I am needing to do. So forgive me if it is in here somewhere.

I am looking to start my script at a certain time. And this part is working fine, I then have it loop 10 times and it shutsdown. That is also fine.

I wanted to clean up the script as I am using long sleep timers to currently accomplish what I am doing. So I figured if I could add time to my start time I would be able to loop it on time that way.

Example: I input start time as 5:00:00 PM, it runs script and the next time I want it to runs is 7:00:00 PM.

Currently I have a sleep of 2hours before the "Until condition.

I would like to get rid of the Do/Until part and make the time function loop it properly.

But when I add "2:00:00: to the time it cuts off minutes and seconds and the AM/PM

so if I add 5:10:25 PM + 2:00:00 then I simply get 7

I have tried @hour methods and other such thing and there is no _TimeAdd like there is _DateAdd

#include <GUIConstantsEx.au3>

#include <Date.au3>

$MyString = _NowTime () ;+ "00:00:15"

$MyString2 = ($Mystring + "2 @HOUR")

GUICreate("Hello World", 200, 100)

GUICtrlCreateLabel($MyString, 50, 10)

GUICtrlCreateLabel($MyString2, 70, 70, 70, 70)

GUISetState(@SW_SHOW)

Sleep(5000)

That is a simple hello world I modified so you can see what I am trying to accomplish. ATM it has @hour but I get the same with just 2 or 2:00:00

Link to comment
Share on other sites

You can do that easily with the _DateAdd function. There is an example in the helpfile for that function that adds 15 minutes to the current time - this can be easily modified to add 2 hours:

#Include <Date.au3>

; Add 2 hours to current time
$sNewDate = _DateAdd( 'h',2, _NowCalc())
MsgBox( 4096, "", "Current time +2 hours: " & $sNewDate )

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Thanks did not know _DateAdd worked for hours. However in the example below I try to incorporate it in my string. It won't even compile. Perhaps I am doing it wrong, I will look into DateAdd functions now that I know it can do just hours.

#include <GUIConstantsEx.au3>
#include <Date.au3>


$MyString = _NowTime () ;+ "00:00:15"
$MyString2 = ($Mystring + _DateAdd( 'h' ,2, _NowCalc())
GUICreate("Hello World", 200, 100)
GUICtrlCreateLabel($MyString, 50, 10)
GUICtrlCreateLabel($MyString2, 70, 70, 70, 70)
GUISetState(@SW_SHOW)
Sleep(5000)

Also if I do it exactly the way they show in the tutorial (the thing you posted) it also give the date not just the time. I am looking for just time for my script to function properly.

Edited by SiliconeClone
Link to comment
Share on other sites

You either needed to add a parentheses at the end of line 6 or remove the first parentheses. Also, if you are trying to combine strings, you use "&" not "+"...this makes is a mathematical computation (add).

You can try this, but I don't know if it is what you are looking for:

#include <GUIConstantsEx.au3>
#include <Date.au3>


$MyString = _NowTime () ;+ "00:00:15"
$MyString2 = $Mystring & @LF & _DateAdd( 'h' ,2, _NowCalc())
GUICreate("Hello World", 200, 100)
GUICtrlCreateLabel($MyString, 50, 10)
GUICtrlCreateLabel($MyString2, 50, 40, 120, 70)
GUISetState(@SW_SHOW)
Sleep(5000)
Link to comment
Share on other sites

Thanks that fixed the compiling /sigh

This is kind of close but your way shows the current time and then 2 hours from now (and still the date). If I had my script on me this second I could post it to better explain what I am trying to do. Basically your line would work if I could get rid of the date from the string somehow

Link to comment
Share on other sites

You could also use _DateDiff to compare two _NowCalc() 's and see how many time has passed (difference can be calculated in anything from years to seconds). Then you would only have to do a loop like this:

#include <Date.au3>

$initialDateTimeStamp = _NowCalc()
$lastDateTimeStamp = _NowCalc()
$currentDateTimeStamp = _NowCalc()

$intervalInSeconds = 5

ConsoleWrite("Initial date/time stamp: " & $initialDateTimeStamp & @CRLF)

While 1
    $currentDateTimeStamp = _NowCalc()
    If _DateDiff('s', $lastDateTimeStamp, $currentDateTimeStamp) >= $intervalInSeconds Then
        ConsoleWrite("Another " & $intervalInSeconds & " seconds have passed!" & @CRLF)
        ConsoleWrite("Difference with last time stamp: " & _DateDiff('s', $lastDateTimeStamp, $currentDateTimeStamp) & @CRLF)
        ConsoleWrite("Difference with initial time stamp: " & _DateDiff('s', $initialDateTimeStamp, $currentDateTimeStamp) & @CRLF)
        ConsoleWrite("Current date/time: " & $currentDateTimeStamp & @CRLF)
        $lastDateTimeStamp = $currentDateTimeStamp
    EndIf
    Sleep(100)
WEnd

Using time difference calculation and ">=", you would also be protected against situations where your computer has fallen asleep or happens to be busy for a full second or the script is paused or whatever. Not sure if your current script passes that check as well, but I have the feeling that it wouldn't... :graduated:

(/EDIT: Ofcourse my script does 5 second intervals instead of 2 hour intervals but that is because it is an example :( Modification to go to hours is easy, either increase the number of interval seconds or make it calculate hours... :D)

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Thanks that fixed the compiling /sigh

This is kind of close but your way shows the current time and then 2 hours from now (and still the date). If I had my script on me this second I could post it to better explain what I am trying to do. Basically your line would work if I could get rid of the date from the string somehow

Then try this:
#include <GUIConstantsEx.au3>
#include <Date.au3>


$MyString = _NowTime () ;+ "00:00:15"
$MyString2 = _DateAdd( 'h' ,2, _NowCalc())
$MyString2 = _DateTimeFormat($MyString2, 3) ;Formats $MyString2 to PC's Time format
$MyString2 = StringReplace($MyString2, _NowCalcDate(), "")  ;removes Date from $MyString2
GUICreate("Hello World", 200, 100)
GUICtrlCreateLabel($MyString, 50, 10)
GUICtrlCreateLabel($MyString2, 50, 40, 120, 70)
GUISetState(@SW_SHOW)
Sleep(5000)
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...