Jump to content

How can I subtract one time (say 22:05:58) from the current time (say 08:02:22) correctly?


Recommended Posts

Posted

VERY new to this so I'll try my best to try to find things in the help as best I can but this one has me a little flustered since I'm certain that it's simple as hell and I just don't see it.

I'm simply logging the current date and time to a file every 30 minutes. In it's current state it's doing this to a loacl file but when it's working I want it to go to a network share but that's not what this post is about...

Each time it reads the current time into a var, I want it to compare the last time that it was written to the current time and then if that's over the expected value, it writes a comment into the file before it writes the new current date/time.

I was just grabbing the last line and grabbing the time which comes back correctly but when I subtract the current time to the last time it always comes back as 0 since it's only doing the hour and not the minutes and seconds.

Is there a specific way to say to use the entire time? 11:38:08 - 11:08:28 does not equal 0. lol

Posted

If you already have some code then please post so we can check where the problem is.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted

$i = 1
While $i < 10
;Get Current Date and Time
   $time = @hour & ":" & @MIN & ":" & @SEC
   $date = @MON & "/" & @MDAY & "/" & @YEAR
;MsgBox(4096, "Date-Time", $date & " - " & $time)
;Create a combined date/time
   $datetime = $date & @TAB & $time
;MsgBox(4096, "Date/Time", $datetime)

$file = FileOpen("test.txt", 0)
; Check if file opened for writing OK
If $file = -1 Then
  MsgBox(0, "Error", "Unable to open file.")
  Exit
EndIf
$last_line = FileReadLine ($file, -1)
;MsgBox(4096, "LastLine", $last_line)
$avParts = StringSplit($last_line, @TAB)
;MsgBox(4096, "LastLine", $avParts[2])
$last_time = $avParts[2]
MsgBox(4096, "Last_Time", $avParts[2])
MsgBox(4096, "Time", $time)
$elapsedtime = $time - $avParts[2]
MsgBox(4096, "Elapsed", $elapsedtime)
FileClose($file)
$file = FileOpen("test.txt", 1)
; Check if file opened for writing OK
If $file = -1 Then
  ;MsgBox(0, "Error", "Unable to open file.")
  Exit
EndIf
If $elapsedtime > 1 Then
  FileWriteLine ($file, "NO CONNECTIVITY FOR" & " " & $elapsedtime & "MINUTES")
EndIf
FileWriteLine($file, $datetime)
FileClose($file)
Sleep(121000)
WEnd
Exit

Posted

If you can store all dates as date and time you can use function _DateDiff to calculate the difference. Please check the help file for how the function works.

Make sure to store date/time as "YYYY/MM/DD HH:MM:SS" - so the tab you use has to be replaced by a space.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted

Thanks!!! I got done what I needed done. That made it sooo much easier. Here's what I came up with. It seems to work ok but I'll test it quite a bit before putting it where it needs to go.

<P>#include &lt;date.au3&gt;</P>
<P><BR>While 1<BR> ; Get current Date/Time<BR>   $datetime =
@YEAR &amp; "/" &amp; @MON &amp; "/" &amp; @MDAY &amp; " " &amp; @HOUR &amp; ":"
&amp;  @MIN &amp; ":" &amp; @SEC<BR>   ;MsgBox(4096, "Date/Time",
$datetime)</P>
<P><BR> ; Open file to read last date/time entry<BR> $file =
FileOpen("test.txt", 0)</P>
<P> ; Check if file opened for reading OK<BR> If $file = -1
Then<BR>    While
1<BR>  Sleep(50000)<BR>  $file = FileOpen("test.txt",
0)<BR>  ;MsgBox(4096, "Testing", "I'm Line
26!")<BR>  ExitLoop<BR>    WEnd<BR> EndIf</P>
<P><BR> ;MsgBox(4096, "Testing", "I'm Line 31!")</P>
<P> $last_line = FileReadLine ($file, -1)<BR> ;MsgBox(4096,
"LastLine", $last_line)</P>
<P> ; Calculate difference between the current Date/Time and the last
successful entry in the log<BR> $elapsedseconds = _DateDiff ("s",
$last_line, $datetime)<BR> ;MsgBox(4096, "ElapsedSec", $elapsedseconds)</P>
<P> $elapsedhours = $elapsedseconds / 3600<BR> ;MsgBox(4096,
"ElapsedHrs", $elapsedhours)</P>
<P> FileClose($file)</P>
<P> ; Open file to write current Date/Time and any notations concerning
extended outages<BR> $file = FileOpen("test.txt", 1)</P>
<P> ;Check if file opened for writing OK<BR> If $file = -1
Then<BR>  While 1<BR>   
Sleep(50000)<BR>    $file = FileOpen("test.txt",
1)<BR>    MsgBox(4096, "Testing", "I'm Line
53!")<BR>    ExitLoop<BR>  WEnd<BR> EndIf</P>
<P> ;If last entry was more than 25 minutes ago notate the file of that
fact<BR> If $elapsedseconds &gt; 1500 Then<BR>  FileWriteLine
($file, "NO CONNECTIVITY FOR" &amp; " " &amp; $elapsedhours &amp; "
HOURS")<BR> EndIf</P>
<P> ;Write the current Date/Time to file<BR> FileWriteLine($file,
$datetime)</P>
<P> ;MsgBox(4096, "Testing", "I'm Line 63!")</P>
<P> FileClose($file)</P>
<P><BR> ; Wait 20 minutes before processing program
again<BR> Sleep(1200000)<BR>WEnd</P>
<P>Exit</P>

#include <date.au3>

While 1

; Get current Date/Time

$datetime = @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC

;MsgBox(4096, "Date/Time", $datetime)

; Open file to read last date/time entry

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK

If $file = -1 Then

While 1

Sleep(50000)

$file = FileOpen("test.txt", 0)

;MsgBox(4096, "Testing", "I'm Line 26!")

ExitLoop

WEnd

EndIf

;MsgBox(4096, "Testing", "I'm Line 31!")

$last_line = FileReadLine ($file, -1)

;MsgBox(4096, "LastLine", $last_line)

; Calculate difference between the current Date/Time and the last successful entry in the log

$elapsedseconds = _DateDiff ("s", $last_line, $datetime)

;MsgBox(4096, "ElapsedSec", $elapsedseconds)

$elapsedhours = $elapsedseconds / 3600

;MsgBox(4096, "ElapsedHrs", $elapsedhours)

FileClose($file)

; Open file to write current Date/Time and any notations concerning extended outages

$file = FileOpen("test.txt", 1)

;Check if file opened for writing OK

If $file = -1 Then

While 1

Sleep(50000)

$file = FileOpen("test.txt", 1)

MsgBox(4096, "Testing", "I'm Line 53!")

ExitLoop

WEnd

EndIf

;If last entry was more than 25 minutes ago notate the file of that fact

If $elapsedseconds > 1500 Then

FileWriteLine ($file, "NO CONNECTIVITY FOR" & " " & $elapsedhours & " HOURS")

EndIf

;Write the current Date/Time to file

FileWriteLine($file, $datetime)

;MsgBox(4096, "Testing", "I'm Line 63!")

FileClose($file)

; Wait 20 minutes before processing program again

Sleep(1200000)

WEnd

Exit

Posted

Just a small enhancement:

$datetime = @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC
can be shortened to
$datetime = _NowCalc()

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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
  • Recently Browsing   0 members

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