Jump to content

How to add minutes?


Recommended Posts

Hi folks.

I'm trying to write a function that looks like this (see below).

I thought it would be simple, but it proves to be a bit more tricky.

Maybe I'm using the wrong approach.

Can somebody point me into the right direction?

What this piece of code should do is commented in the script.

Thanks in advance.

Scriptonize

#include <Date.au3>

Dim $ShutDownAt="10:07"
Dim $TimeNowIs


ConsoleWrite("Running..." & @CR)
ConsoleWrite("$ShutDownAt: " & $ShutDownAt & @CR)

While 1
        
    $TimeNowIs=_NowTime(4)                             ;Get system Time in 24 hour format
    ConsoleWrite("$TimeNowIs: " & $TimeNowIs & @CR)     ;Write this value into console
    
    If  $TimeNowIs < $ShutDownAt Then                     ;Is it time to shutdown this app?
        ConsoleWrite("ShutDown now? No, to early" & @CR);If not, update console
    EndIf


; If the time to shutdown has passed one minute more then 
; specified, shut down app. 
; If the shutdown time has passed more then one minute after 
; defined shutdown time, don't shutdown, but continue!!!!

    ConsoleWrite("Calculated time: " & $ShutDownAt+1)       
    If  $TimeNowIs = $ShutDownAt+1 Then                         
        ConsoleWrite($ShutDownAt+1 & " Time to shutdown" & @CR);Update console
        Exit
    EndIf
    
    Sleep(5000)
    
WEnd
Edited by Scriptonize

If you learn from It, it's not a mistake

Link to comment
Share on other sites

hi

its in the help file??

#include <Date.au3>

; Add 5 days to today

$sNewDate = _DateAdd( 'd',5, _NowCalcDate())

MsgBox( 4096, "", "Today + 5 days:" & $sNewDate )

; Subtract 2 weeks from today

$sNewDate = _DateAdd( 'w',-2, _NowCalcDate())

MsgBox( 4096, "", "Today minus 2 weeks: " & $sNewDate )

; Add 15 minutes to current time

$sNewDate = _DateAdd( 'n',15, _NowCalc())

MsgBox( 4096, "", "Current time +15 minutes: " & $sNewDate )

; Calculated eventlogdate which returns second since 1970/01/01 00:00:00

$sNewDate = _DateAdd( 's',1087497645, "1970/01/01 00:00:00")

MsgBox( 4096, "", "Date: " & $sNewDate )

Link to comment
Share on other sites

@nobbe

Thanks man,

I checked the help file, but I missed that one. How come?

I was searching for Time Calculation, I didn't check the function named "_DateAdd".

Next time I better widen my scope.

I've found a solution to my problem (thanx to you).

here is my modified (working) example:

#include <Date.au3>

Dim $TimeNowIs
Dim $ShutDownAt=@YEAR & "/" & @MON & "/" & @MDAY &  " " & "12:10"
$ShutDownAt2 = _DateAdd( 'n',2, $ShutDownAt)
$ShutDownAt2 = _DateTimeFormat($ShutDownAt2,4)

ConsoleWrite("Running..." & @CR)
ConsoleWrite("$ShutDownAt: " & $ShutDownAt & @CR)
ConsoleWrite("$ShutDownAt2: " & $ShutDownAt2 & @CR)

While 1
        
    $TimeNowIs=_NowTime(4)                              ;Get system Time in 24 hour format
    ConsoleWrite("$TimeNowIs: " & $TimeNowIs & @CR)             ;Write this value into console
    
    If  $TimeNowIs < $ShutDownAt Then                       ;Is it time to shutdown this app?
        ConsoleWrite("ShutDown now? No, to early" & @CR)        ;If not, update console
    EndIf

    ;If the time to shutdown has passed one minute more then 
        ;specified, shut down app. 
    ;If the shutdown time has passed more then one minute after 
        ;defined shutdown time, don't shutdown, but continue!!!!
    
    If  $TimeNowIs = $ShutDownAt2 Then                          
        ConsoleWrite($ShutDownAt2 & " Time to shutdown" & @CR)      ;Update console
        ExitLoop
    EndIf
    
    Sleep(5000)
    
WEnd
Exit

One thing I noticed is that while using option 'n' you need to add one more minute as actually needed.

Example;

Add 1 minute to a given time you need code like this:

$ShutDownAt2 = _DateAdd( 'n',2, $MyDateTime)

I expected it to be:

$ShutDownAt2 = _DateAdd( 'n',1, $MyDateTime)

No big deal as long as you know this.

If you learn from It, it's not a mistake

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