KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to calculate the difference between two ISO DATE formatted strings
PRODUCT: 4D | VERSION: 12.1 | PLATFORM: Mac & Win
Published On: December 17, 2010

The following method can be used to calculate the difference (in days, hours, minutes, and seconds) between two ISO DATE formatted strings:

// Description:
// Calculates the difference between two ISO DATE timestamps values
// (2010-11-22T15:55:22) and returns human readable string value of
// elapse time in "Days, Hours, Minutes, Seconds"
//
// Parameters:
// $1 = string; start time = ISO DATE timestamp (now if empty string is passed)
// $2 = string; end time = ISO DATE timestamp (now if empty string is passed)
//
// Return Value:
// $0 = string; time difference in "Days, Hours, Minutes, Seconds"
//


C_TEXT($0)

If (Count parameters=2)

C_TEXT($1;$sts) // start timestamp
C_TEXT($2;$ets) // end time stamp


If ((Position("z";$1)=0) & (Position("z";$2)=0))
// "z" is not found in $1 or $2

If($1="")
// start time is empty - so use now
$sts:=String(Current date;ISO Date;Current time)
Else
// start time is not empty - so confirm the format
// for now just assign it

$sts:=String(Date($1);ISO Date;Time($1))
End if

If ($2="")
// end time is empty - so use now
$ets:=String(Current date;ISO Date;Current time)
Else
// start time is not empty - so confirm the format
// for now just assign it

$ets:=String(Date($2);ISO Date;Time($2))
End if

C_LONGINT($days_l)
C_TIME($hms_t)

If ($ets>$sts)
$days_l:=Date($ets)-Date($sts)
$hms_t:=Time($ets)-Time($sts)
Else
$days_l:=Date($sts)-Date($ets)
$hms_t:=Time($sts)-Time($ets)
End if

If ($hms_t<?00:00:00?)
// time is negative so correct the days
$days_l:=$days_l-1
// subtract 1 day from days
$hms_t:=$hms_t+?24:00:00?
// add 24 hours to time
End if

If ($days_l=0)
// zero days
$0:=String($hms_t;Hour Min Sec)
Else
// not zero days
$0:=String($days_l;"###,##0 days ")+String($hms_t;Hour Min Sec)
End if

Else
// "z" is found in $1 or $2
// this method does not accept ISO DATE GMT - only ISO DATE

$0:="This method requires ISO DATE format parameters only"
End if

Else
// incorrect parameters
// this method requires 2 parameters (check the header)

$0:="This method requires 2 parameters"
End if



If the method above is saved as UTIL_CalcTimeOffset, it can be used in the following manner:

C_TEXT($test)
$test:=UTIL_CalcTimeOffset ("2010-11-22T22:45:45";"2010-11-23T01:56:32")
// 3 hours, 10 minutes and 47 seconds

C_TEXT($test)
$test:=UTIL_CalcTimeOffset ("2010-11-23T01:56:32";"2010-11-22T22:45:45")
// 3 hours, 10 minutes and 47 seconds

C_TEXT($test)
$test:=UTIL_CalcTimeOffset ("2002-04-16T10:00:00";"2010-11-23T08:25:45")
// 3142 days, 22 hours, 25 minutes and 45 seconds

C_TEXT($test)
$test:=UTIL_CalcTimeOffset ("2010-11-23T08:25:45";"2002-04-16T10:00:00")
// 3142 days, 22 hours, 25 minutes and 45 seconds

C_TEXT($test)
$test:=UTIL_CalcTimeOffset ("2010-11-23T22:45:45";"2010-11-23T01:56:32")
// 20 hours 49 minutes 13 seconds

C_TEXT($test)
$test:=UTIL_CalcTimeOffset ("2010-11-23T01:56:32";"2010-11-23T22:45:45")
// 20 hours 49 minutes 13 seconds

See Also: