#83904 - 03/03/11 03:27 PM
DateTimeDiff() - Calculates the time difference between two date-times-groups
|
Sealeopard
Moderator
   
Registered: 01/04/25
Posts: 11143
Loc: Boston, MA, USA
|
code:
;FUNCTION DateTimeDiff ; ;ACTION Calculates the time difference between two date-times-groups ; ;AUTHOR Jens Meyer (sealeopard@usa.net) ; ;VERSION 1.1 ; ;KIXTART 4.20 ; ;SYNTAX DATETIMEDIFF(DTG1 [,DTG2]) ; ;PARAMETYERS DTG1 ; Required string as a date-time-group (DTG) in one of the following formats: ; "YYYY/MM/DD HH:MM[:SS.MSECS]" ; "YYYY/MM/DD" which assumes that HH:MM[:SS.MSECS] = 00:00:00.000 ; "HH:MM[:SS.MSECS]" which assumes that "YYY/MM/DD" = @DATE ; ; DTG2 ; Optional string as date-time-group (DTG) in one of the following formats: ; "YYYY/MM/DD HH:MM[:SS.MSECS]" ; "YYYY/MM/DD" which assumes that HH:MM[:SS.MSECS] = @TIME ; "HH:MM[:SS.MSECS]" which assumes that "YYY/MM/DD" = @DATE ; DTG2 will default to the current date and time if not provided in the form of ; @DATE+' ' +@TIME+'.'+@MSECS ; ; ; ;RETURNS Returns difference DTG2-DTG1 as "YYYY/MM/DD HH:MM[:SS.MSECS]" ; ;REMARKS none ; ;DEPENDENCIES SERIALTIME() @ http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic&f=12&t=000039 ; SERIALDATE() @ http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic&f=12&t=000089 ; ;EXAMPLE $rc=DATETIMEDIFF('23:20:33','01:01:01.245') ; ;KIXTART BBS http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic&f=12&t=000374 ; function datetimediff($time1,$time2) Dim $date1, $date2, $datediff, $timediff
$datetimediff=''
if not instr($time1,' ') select case instr($time1,'/') $time1=$time1+' 00:00:00.000' case instr($time1,':') $time1=@DATE+' '+$time1 case 1 exit 87 endselect endif
if not instr($time2,' ') select case instr($time2,'/') $time2=$time2+' '+@TIME+'.'+right('000'+@MSECS,3) case instr($time2,':') $time2=@DATE+' '+$time2 case 1 $time2=@DATE+' '+@TIME+'.'+right('000'+@MSECS,3) endselect endif
$time1=split($time1,' ') $date1=$time1[0] $time1=$time1[1] $time2=split($time2,' ') $date2=$time2[0] $time2=$time2[1]
$datediff=serialdate($date2)-serialdate($date1) $timediff=serialtime($time2)-serialtime($time1) if $timediff=>86400 $timediff=$timediff-86400 $datediff=$datediff+1 endif $datediff=serialdate(serialdate('0000/00/00')+$datediff-1) $timediff=serialtime($timediff)
$datetimediff=$datediff+' '+$timediff exit 0 endfunction
[ 11. March 2003, 15:28: Message edited by: sealeopard ]
_________________________
There are two types of vessels, submarines and targets.
|
|
Top
|
|
|
|
#83905 - 05/03/07 09:26 PM
Re: DateTimeDiff() - Calculates the time difference between two date-times-groups
|
jtokach
Seasoned Scripter
   
Registered: 01/11/15
Posts: 513
Loc: PA, USA
|
This UDF has a major issue and demostrated by: Code:
datetimediff("2005/03/04 15:00:16","2005/03/07 14:20:12") The code should be changed to: (comments denote addition) Code:
function datetimediff($time1,$time2) Dim $date1, $date2, $datediff, $timediff
$datetimediff=''
if not instr($time1,' ') select case instr($time1,'/') $time1=$time1+' 00:00:00.000' case instr($time1,':') $time1=@DATE+' '+$time1 case 1 exit 87 endselect endif
if not instr($time2,' ') select case instr($time2,'/') $time2=$time2+' '+@TIME+'.'+right('000'+@MSECS,3) case instr($time2,':') $time2=@DATE+' '+$time2 case 1 $time2=@DATE+' '+@TIME+'.'+right('000'+@MSECS,3) endselect endif
$time1=split($time1,' ') $date1=$time1[0] $time1=$time1[1] $time2=split($time2,' ') $date2=$time2[0] $time2=$time2[1]
$datediff=serialdate($date2)-serialdate($date1) $timediff=serialtime($time2)-serialtime($time1) ; added to handle negative values from $timediff if $timediff<0 $datediff=$datediff-1 $timediff=$timediff+86400 ;adding the negative and a positive give the proper diff endif ; end addition if $timediff=>86400 $timediff=$timediff-86400 $datediff=$datediff+1 endif $datediff=serialdate(serialdate('0000/00/00')+$datediff-1) $timediff=serialtime($timediff) $datetimediff=$datediff+' '+$timediff exit 0 endfunction
_________________________
-Jim
...the sort of general malaise that only the genius possess and the insane lament.
|
|
Top
|
|
|
|
#83906 - 05/03/15 04:59 PM
Re: DateTimeDiff() - Calculates the time difference between two date-times-groups
|
annie
Fresh Scripter
Registered: 02/02/12
Posts: 10
|
quick question about date difference UDF. I need to be able to check todays date vs a date of a file, and if today is > by 6 months than date of file, then call a program. is it better to use datecalc or datetimediff, and how can I strip off the time part because I really don't need it? thanks for your help!
|
|
Top
|
|
|
|
#83908 - 05/03/16 09:06 AM
Re: DateTimeDiff() - Calculates the time difference between two date-times-groups
|
Jochen
Moderator
   
Registered: 00/03/17
Posts: 5593
Loc: Stuttgart, Germany
|
Hi Annie,
answering anyway, hmmm, a mod may remove this unrelated chat later on 
In your specific case (age>6 months) I'd use [shameless selfadvertising]datecalc()[/advertising] for two reasons: 1. No timediff is needed 2. There are no dependant udfs attached to it
The sample section should give you a clue on how to use it, but if there are further question I have to ask you to open your own thread in starters forum
|
|
Top
|
|
|
|
Moderator: Jochen, Radimus, Sealeopard, Bryce, Kdyer, Howard Bullock, Chris S., Glenn Barnas, Allen, Benny69, Mart
|
2 registered
(Allen, bugmen)
and 36 anonymous users online.
|
|
|