DateDiff ?
DateDiff ?
I have a little problem to calculate the time different between two days/times. If someone can show me a working way, it would be nice. What i need is something like:
Procedure Diff (sTimeStamp1.s, sTimeStamp2.s) ; sTimeStamp.s = like 1166173200
Result = "x Years, x Months, x Days, x Hours, x Minutes, x Seconds"
thanks.
Procedure Diff (sTimeStamp1.s, sTimeStamp2.s) ; sTimeStamp.s = like 1166173200
Result = "x Years, x Months, x Days, x Hours, x Minutes, x Seconds"
thanks.
va!n aka Thorsten
Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
Code: Select all
DateDifference = DateLate-DateEarly
Code: Select all
Procedure.l YearDiff(Date)
ProcedureReturn Year(Date)-1970
EndProcedure
Procedure.l MonthDiff(Date)
ProcedureReturn Month(Date)-1
EndProcedure
Procedure.l DayDiff(Date)
ProcedureReturn Day(Date)-1
EndProcedure
Debug YearDiff(DateDifference)
Debug MonthDiff(DateDifference)
Debug DayDiff(DateDifference)
Debug Hour(DateDifference)
Debug Minute(DateDifference)
Debug Second(DateDifference)
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Won't work, Trond, because the pattern of months will rarely line up properly. Take a six-month date difference, the months will always be assumed to be jan-feb-mar-apr-may-june, which will affect the number of days returned. See the error with these dates:
Too many days! Also - leap years counted in the wrong place will throw it off by a whole day for longer differences.
Code: Select all
dateearly = ParseDate("%yyyy/%mm/%dd/%hh:%ii:%ss", "2006/6/01/00:00:00")
datelate = Date()
BERESHEIT
-
- New User
- Posts: 2
- Joined: Wed Sep 06, 2006 11:37 am
- Location: europe
This code will return the time difference as string.
Code: Select all
; TimeDiffString : jear Dez 2005
;#TimeUnits = "Woche|n,Tag|e,Stunde|n,Minute|n,Sekunde|n"
#TimeUnits = "week|s,day|s,hour|s,minute|s,second|s"
Procedure.s AddTimeUnit(number.l, unit.l)
Protected Result.s, sUnit.s
If number = 0 : ProcedureReturn "" : EndIf
If number < 0 : number * -1 : EndIf
sUnit = StringField(#TimeUnits, unit, ",")
If number > 1
sUnit = RemoveString(sUnit, "|")
Else
sUnit = StringField(sUnit, 1, "|")
EndIf
Result + Space(1) + Str(number) + Space(1) + sUnit
ProcedureReturn Result
EndProcedure
Procedure.s sTimeDiff(Seconds.l)
Protected Result.s
Protected Weeks.l, Days.l, Hours.l, Minutes.l
Weeks = Seconds / 604800 : Seconds = Seconds % 604800
Days = Seconds / 86400 : Seconds = Seconds % 86400
Hours = Seconds / 3600 : Seconds = Seconds % 3600
Minutes = Seconds / 60 : Seconds = Seconds % 60
Result = AddTimeUnit(Weeks,1)
Result + AddTimeUnit(Days,2) : Result + AddTimeUnit(Hours,3)
Result + AddTimeUnit(Minutes,4) : Result + AddTimeUnit(Seconds,5)
ProcedureReturn Result
EndProcedure
PastDate.l = ParseDate("%dd.%mm.%yyyy", "20.12.2005 12:00")
Debug Date() - PastDate
Debug sTimeDiff(Date() - PastDate)
Delay(2000)
Debug PastDate - Date()
Debug sTimeDiff(PastDate - Date())
@jear:
Your routine gives not the correct results that was requested by vain.
Take a look on the Tips&Tricks section:
http://www.purebasic.fr/english/viewtopic.php?t=25085
Your routine gives not the correct results that was requested by vain.
Take a look on the Tips&Tricks section:
http://www.purebasic.fr/english/viewtopic.php?t=25085
Tranquil
You can use the following code to compute the day difference.
The Debug Output window displays:
Now all that remains is for you to do the time (clock) computation (and, if necessary, subtract a day from the day difference computation).
You might also find (the last part of) this thread interesting: As a sidenote, you might be wondering why I choose to use Int() in the Days() function. Well, it's because it's easier for me to write those to ensure that I get integer arithmetic than to constantly try to figure out whether PureBasic will automatically apply integer arithmetic to the computation. Not a big deal, I know, but one needs to be constantly on one's toes to avoid inadvertent use of the float library. I asked for it before (early 2006, I believe) and I noticed that others have recently requested the same in the Wishlist sub-forum: PLEASE, PLEASE, PLEASE give us an integer division operator (eg Div).
Code: Select all
Procedure.l Days(Year.l, Month.l, Day.l)
; CONVERT DATE PARTS TO JULIAN DAY (SERIAL) NUMBER:
A.l = (14 - Month.l) / 12
Y.l = Year.l + 4800 - A.l
M.l = Month.l + (12 * A.l) - 3
D.l = Day.l + Int((153 * M.l + 2) / 5) + (365 * Y.l) + Int(Y.l / 4)
ProcedureReturn = D.l - Int(Y.l / 100) + Int(Y.l / 400) - 32045
EndProcedure
Debug days(2004,3,1) - days(2004,2,28) ; 2004 was a leap year
Debug days(2001,1,1) - days(2000,1,1) ; 2000 was a leap year
Debug days(1901,1,1) - days(1900,1,1) ; 1900 was not a leap year
Debug days(2000,1,1) - days(1900,1,1) ; 24 leap years between centennials
Debug days(2100,1,1) - days(2000,1,1) ; ... unless least centennial is divisible by 400
Debug days(1908,7,19) - days(756,3,29)
- 2
366
365
36524
36525
420871

Now all that remains is for you to do the time (clock) computation (and, if necessary, subtract a day from the day difference computation).
You might also find (the last part of) this thread interesting: As a sidenote, you might be wondering why I choose to use Int() in the Days() function. Well, it's because it's easier for me to write those to ensure that I get integer arithmetic than to constantly try to figure out whether PureBasic will automatically apply integer arithmetic to the computation. Not a big deal, I know, but one needs to be constantly on one's toes to avoid inadvertent use of the float library. I asked for it before (early 2006, I believe) and I noticed that others have recently requested the same in the Wishlist sub-forum: PLEASE, PLEASE, PLEASE give us an integer division operator (eg Div).
-
- Enthusiast
- Posts: 767
- Joined: Sat Jan 24, 2004 6:56 pm
-
- PureBasic Expert
- Posts: 4229
- Joined: Sat Apr 26, 2003 8:27 am
- Location: Strasbourg / France
- Contact:
I used this one in an old project :
It seems to pass the Lewis test !
Code: Select all
Procedure.l Julian(year.l, month.l, day.l)
Protected Days.l, yearsBC.l, yearsAD.l
If month < 3
month = month + 12
year = year - 1
EndIf
yearsBC = 4714 - 1
yearsAD = year - 1
Days = Int((yearsBC + yearsAD) * 365.25)
Days = Days - (year / 100)
Days = Days + (year / 400)
Days = Days + Int(30.6 * (month - 1) + 0.2)
ProcedureReturn Days + day
EndProcedure
2
366
365
36524
36525
420871
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
>> Have you considered using the mod-operator (%) to create a DIV ?
>
> I'm sure you're just dying to enlighten me, so go ahead!
http://www.purebasic.com/documentation/ ... ables.html
>
> I'm sure you're just dying to enlighten me, so go ahead!

http://www.purebasic.com/documentation/ ... ables.html
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
"PureBasic won't be object oriented, period" - Fred.
What nonsense is this? Why on earth would you think that directing me to the modulo entry in the PureBasic Reference Manual would make me any the wiser?PB wrote:>> Have you considered using the mod-operator (%) to create a DIV ?
>
> I'm sure you're just dying to enlighten me, so go ahead!
http://www.purebasic.com/documentation/ ... ables.html
