WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Time UDFs

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Force Valid TimeDiff Strings

 Keywords: TimeDiff Valid Format Date Time YMDHMS 

Question:

Quick question. I have 4 time events (lets call them A,B,C and D). I can use timediff to get the time difference between A and D, which comes back as 0000:00:00:00:05:32 and then do a timediff between B and C which comes back as 0000:00:00:00:03:10, however, when I use timediff to get the difference between the 2 results it gives an error: "1462: TimeSubtract: Cannot subtract supplied times".

Now both numbers are in the right format YMDHMS, and the first number (A to D) is always going to be higher (therefore later) then the second (B to C). Can anyone shed any light on this? Is it simply that the YMD part of the time has to have numbers?

Answer:

The values you pass to TimeDiff: "datetime1" and "datetime2" must be valid date-time strings.

For Example:

diff1 = TimeDiff (TimeYmdHms(), "1993:10:05:00:00:00")
diff2 = TimeDiff (TimeYmdHms(), "2009:02:01:00:00:00")
diff3 = TimeDiff (diff1, diff2)
... and this too ...
diff = TimeDiff("0000:01:01:00:05:32", "0000:01:01:00:03:10") 
The time calculation begins in year zero, at the first day in January, at hour zero, minute zero, second zero. Here is UDF to force valid datetime strings.
#DefineFunction udfTimeDiff (strYmdHms1, strYmdHms2)
; Make valid datetime strings.
;Detlev Dalitz DD.20090611.1800.CEST
strYmdHms1 = TimeAdd ("0:1:1", strYmdHms1)
strYmdHms2 = TimeAdd ("0:1:1", strYmdHms2)
; Compare datetime strings.
If strYmdHms1 > strYmdhms2 Then Return TimeDiff (strYmdHms1, strYmdhms2)
Return TimeDiff (strYmdHms2, strYmdhms1)
#EndFunction

; Test.

strYmdHms1 = "0:0:0:0:5:32"
strYmdHms2 = "0:0:0:0:3:10"

strYmdHmsDiff1 = udfTimeDiff (strYmdHms1, strYmdHms2) ; "0000:00:00:00:02:22"
strYmdHmsDiff2 = udfTimeDiff (strYmdHms2, strYmdHms1) ; "0000:00:00:00:02:22"

Exit

Article ID:   W18404
Filename:   Force Valid TimeDiff Strings.txt
File Created: 2009:06:11:10:10:50
Last Updated: 2009:06:11:10:10:50