Page 1 of 1
Need to get the time difference between two dates?
Posted: Thu Sep 23, 2010 12:33 pm
by treebolt
need something like this
Code: Select all
DateDiff (part, date1, date2) ; difference in two dates, part is year, month, day, hour, minute, second
I'm pretty sure this is not built into PureBasic and I can't think of a way to make this.
EDIT: Actually all I really need are hours but I figure once you have the difference in seconds, you can convert to any other format quite easily.
Re: Need to get the time difference between two dates?
Posted: Thu Sep 23, 2010 12:54 pm
by STARGĂ…TE
use the Timestamp of your dates, and:
Code: Select all
Difference = Date(2010, 09, 23, 14, 00, 00) - Date(2010, 09, 2, 5, 30, 50)
Debug Int(Difference/(60*60*24)) ; day
Debug Int(Difference/(60*60)) % (24) ; hour
Debug Int(Difference/(60)) % (60) ; minute
Debug Int(Difference) % (60) ; sekunde
EDIT:
Procedure:
Code: Select all
Procedure DateDifference(Date1, Date2, Field)
Protected Difference = Date1-Date2
Select Field
Case #PB_Date_Second
ProcedureReturn Difference % 60
Case #PB_Date_Minute
ProcedureReturn Int(Difference/60) % 60
Case #PB_Date_Hour
ProcedureReturn Int(Difference/3600) % 24
Case #PB_Date_Day
ProcedureReturn Int(Difference/86400)
EndSelect
EndProcedure
Re: Need to get the time difference between two dates?
Posted: Thu Sep 23, 2010 12:57 pm
by rsts
Not sure how your dates are formatted or what "part" is, but date2-date1 should give you the difference if the dates are available as yr/mo/da/hr/min/sec.
You can use the PB date functions.
cheers
Re: Need to get the time difference between two dates?
Posted: Thu Sep 23, 2010 6:56 pm
by Rook Zimbabwe
I did this for my employee timeclock program of my POS... it is pretty simple.
Are you writing a POS or timesheet program?
I stored all values in a DB file in RAW date() format then i read them when needed and performed tthe following matth on them:
Code: Select all
cin$ = GetDatabaseString(#DatabaseEMPLOYEE,3)
time0 = Val(cin$)
cin$ = FormatDate(mask$,time0)
b1o$ = GetDatabaseString(#DatabaseEMPLOYEE,4)
time1 = Val(b1o$)
b1o$ = FormatDate(mask$,time1)
b1n$ = GetDatabaseString(#DatabaseEMPLOYEE,5)
time2 = Val(b1n$)
b1n$ = FormatDate(mask$,time2)
lunchout$ = GetDatabaseString(#DatabaseEMPLOYEE,6)
time3 = Val(lunchout$)
lunchout$ = FormatDate(mask$,time3)
lunchin$ = GetDatabaseString(#DatabaseEMPLOYEE,7)
time4 = Val(lunchin$)
lunchin$ = FormatDate(mask$,time4)
b2o$ = GetDatabaseString(#DatabaseEMPLOYEE,8)
time5 = Val(b2o$)
b2o$ = FormatDate(mask$,time5)
b2n$ = GetDatabaseString(#DatabaseEMPLOYEE,9)
time6 = Val(b2n$)
b2n$ = FormatDate(mask$,time6)
cout$ = GetDatabaseString(#DatabaseEMPLOYEE,10)
time7 = Val(cout$)
cout$ = FormatDate(mask$,time7)
tsecs$ = GetDatabaseString(#DatabaseEMPLOYEE,11)
status$ = GetDatabaseString(#DatabaseEMPLOYEE,12)
checknumber$ = GetDatabaseString(#DatabaseEMPLOYEE,13)
basetime = time7 - time0 ; seconds between CIN and COUT (clock in and clock OUT!)
b1 = time2 - time1 ; don't forget the breaks!
lunch = time4 - time3
b2 = time5 - time6
subby = b1 + lunch + b2
tsec = basetime - subby
secs$ = Str(tsec)
Seconds = tsec % 60
Minutes = tsec % (60 * 60) / 60
Hours = tsec % (60 * 60 * 60) / (60 * 60)
sec$ = Str(Seconds)
min$ = Str(Minutes)
hrs$ = Str(Hours)
tsecs$ = hrs$+":"+min$+":"+sec$
If you have a client in Colorado... Version 8 is due out but unless she actually READS the instructions and WATCHES your training Videos she is going to make many terrible mistakes!!!

Re: Need to get the time difference between two dates?
Posted: Thu Sep 23, 2010 11:01 pm
by PB
> need something like [DateDiff]
The AddDate() command is the equivalent of DateDiff.
Despite its name, you can subtract dates with it too.
> all I really need are hours
Then you'd use it like this:
Code: Select all
today=Date()
; Get date 24 hours ahead:
tomorrow$=FormatDate("%yyyy %mm %dd",AddDate(today,#PB_Date_Hour,24))
Debug tomorrow$
; Get date 24 hours behind:
yesterday$=FormatDate("%yyyy %mm %dd",AddDate(today,#PB_Date_Hour,-24))
Debug yesterday$
Re: Need to get the time difference between two dates?
Posted: Fri Sep 24, 2010 7:57 pm
by Rook Zimbabwe
Hmm will play with adddate() didn't know about it when I coded original in PB 3.?
FYI: there are 86400 seconds in a day
