Page 1 of 1 1
Topic Options
#83904 - 03/03/11 03:27 PM DateTimeDiff() - Calculates the time difference between two date-times-groups
Sealeopard Moderator Offline
Moderator
*****

Registered: 01/04/25
Posts: 11153
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 Offline
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 Offline
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
#83907 - 05/03/15 05:39 PM Re: DateTimeDiff() - Calculates the time difference between two date-times-
Jose Offline
Seasoned Scripter
*****

Registered: 01/04/04
Posts: 692
Loc: Buenos Aires - Argentina
Hi annie sup?
You should be posting your questions about udfs at starters forum.

Take a look at this threat about triggering code after a certain time. Here. If you just wish to do somethign without taking into account file age.

Or you should use GetFileTime() and compare it with date macro with this same UDF above.
_________________________
Life is fine.

Top
#83908 - 05/03/16 09:06 AM Re: DateTimeDiff() - Calculates the time difference between two date-times-groups
Jochen Moderator Offline
KiX Supporter
*****

Registered: 00/03/17
Posts: 5653
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
_________________________
I cna ytpe 300 wrods pre mniuet!!

Kixforms forum.
Deutsches KiXtart/Kixforms forum

Top
Page 1 of 1 1


Moderator:  Jochen, Radimus, Sealeopard, Bryce, Kdyer, Howard Bullock, Chris S., Glenn Barnas, Allen, Benny69, Mart 
Hop to:
Shout Box

Who's Online
5 registered (Digi, Mart, It_took_my_meds, Glenn Barnas, Sam_B) and 68 anonymous users online.

Generated in 0.074 seconds in which 0.017 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org