Need to get the time difference between two dates?

Just starting out? Need help? Post your questions and find answers here.
User avatar
treebolt
User
User
Posts: 50
Joined: Thu Feb 21, 2008 4:38 pm
Location: Palm Harbor, FL

Need to get the time difference between two dates?

Post 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.
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Need to get the time difference between two dates?

Post 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 
Last edited by STARGÅTE on Thu Sep 23, 2010 12:59 pm, edited 1 time in total.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Need to get the time difference between two dates?

Post 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
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: Need to get the time difference between two dates?

Post 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? :D

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!!! :mrgreen:
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Need to get the time difference between two dates?

Post 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$
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: Need to get the time difference between two dates?

Post by Rook Zimbabwe »

Hmm will play with adddate() didn't know about it when I coded original in PB 3.? :D

FYI: there are 86400 seconds in a day :mrgreen:
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Post Reply