Compare Dates?

Just starting out? Need help? Post your questions and find answers here.
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Compare Dates?

Post by Killswitch »

Is there a way to work out the number of Years, Months, Days, Hours, Minutes and Seconds between two given dates.

For example

10/05/2005

And right now.
~I see one problem with your reasoning: the fact is thats not a chicken~
Berikco
Administrator
Administrator
Posts: 1326
Joined: Wed Apr 23, 2003 7:57 pm
Location: Belgium
Contact:

Post by Berikco »

Quick one, not tested

Code: Select all

OldDate = Date(1999, 5, 4, 8, 32, 23)
Difference = Date() - OldDate
Debug Str(Year(Difference) - 1970) + " Year / "+ FormatDate("%mm Month / %dd Day   %hh hour %ii min %ss sec", Difference )
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

Yeah, it works for pretty well. Howeverwhen I compare 10/07/05 00:00:00 to Date() I end up with 17 days - is that right? Surely it should be 13?
~I see one problem with your reasoning: the fact is thats not a chicken~
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

then, just remove 4 days from the output :D

I also tried it returns really a bigger number than it should...
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

You have to edit it for the date I specified :D
~I see one problem with your reasoning: the fact is thats not a chicken~
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> not tested

Hehe, it doesn't work at all. It's Christmas Day here, but when I test Christmas Eve...

Code: Select all

OldDate = Date(2005, 12, 24, 0, 0, 0)
Difference = Date() - OldDate
Debug Str(Year(Difference) - 1970) + " Year / "+ FormatDate("%mm Month / %dd Day   %hh hour %ii min %ss sec", Difference )
...it says it's 0 Year / 01 Month / 02 Day 11 hour 05 min 05 sec away. :twisted:
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
DoubleDutch
Addict
Addict
Posts: 3197
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Have you all been at the booze already - your processing it thru a date function... Think about it! :?

These procedures will work it out nicely for you...

Code: Select all

Procedure.s AddNumberS(string$,number,ident$)
  result$=string$
  If number<>0
    result$+" "+Str(number)+" "+ident$
    If number<>1
      result$+"s"
    EndIf
  EndIf
  ProcedureReturn result$
EndProcedure

Procedure.s NiceTime(seconds)
  result$=""
  Weeks=seconds/604800
  seconds=seconds%604800
  result$=AddNumberS(result$,Weeks,"week")
  Days=seconds/86400
  seconds=seconds%86400
  result$=AddNumberS(result$,Days,"day")
  Hours=seconds/3600
  seconds=seconds%3600
  result$=AddNumberS(result$,Hours,"hour")
  Minutes=seconds/60
  seconds=seconds%60
  result$=AddNumberS(result$,Minutes,"minute")
  result$=AddNumberS(result$,seconds,"second")
  If result$=""
    result$=" just now "
  Else
    result$+" "
  EndIf
  result$=Right(result$,Len(result$)-1)
  ProcedureReturn result$
EndProcedure
Pass the date difference (futuredate-pastdate) into nicetime(), you will get a CORRECT string back in return.

Merry Christmas!


-Anthony
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

That is really a good functioning one, thanks doubledutch!
User avatar
DoubleDutch
Addict
Addict
Posts: 3197
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

NP, glad to help. :)
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

Thanks :D
~I see one problem with your reasoning: the fact is thats not a chicken~
Post Reply