Page 1 of 1

Compare Dates?

Posted: Sat Dec 24, 2005 3:52 pm
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.

Posted: Sat Dec 24, 2005 4:16 pm
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 )

Posted: Sat Dec 24, 2005 7:57 pm
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?

Posted: Sat Dec 24, 2005 11:28 pm
by josku_x
then, just remove 4 days from the output :D

I also tried it returns really a bigger number than it should...

Posted: Sun Dec 25, 2005 12:06 am
by Killswitch
You have to edit it for the date I specified :D

Posted: Sun Dec 25, 2005 1:08 am
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:

Posted: Sun Dec 25, 2005 2:00 am
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

Posted: Sun Dec 25, 2005 10:50 am
by josku_x
That is really a good functioning one, thanks doubledutch!

Posted: Mon Dec 26, 2005 12:45 pm
by DoubleDutch
NP, glad to help. :)

Posted: Mon Dec 26, 2005 2:09 pm
by Killswitch
Thanks :D