Page 1 of 2
[Newby] About Date
Posted: Fri Feb 24, 2006 11:39 am
by buzzqw
maybe a very stupid question but i not found a intelligent solution...
how to calculate the time elapsed between two date
I MEAN :
first date 01/03/2004
second date 24/01/2006
time elapsed=1 year , 10 month, 23 days (i hope my calculation is correct...)
THANKS !
BHH
Posted: Fri Feb 24, 2006 12:42 pm
by flaith
With this code you'll have the number of days between dates ->
Code: Select all
; --------------------
; Procédures calcul de date
; Auteur : Paneric
; Date : 22/09/2004
;
;- Procédures
;
Procedure.l NombredeJoursduMois(mois.l, annee.l)
longueurdumois.l = 0
If mois = 4 Or mois = 8 Or mois = 9 Or mois = 11
longueurdumois = 30
Else
If mois = 2
If annee % 4 = 0
If annee%100 = 0 And annee%400 <> 0
longueurdumois = 28
Else
longueurdumois = 29
EndIf
Else
longueurdumois = 28
EndIf
Else
longueurdumois = 31
EndIf
EndIf
ProcedureReturn longueurdumois
EndProcedure
Procedure.l NombredeJoursentreDate(jour1.l, mois1.l, annee1.l, jour2.l, mois2.l, annee2.l)
Intervalle.l = 0
m.l = 0
If annee1 + 1 <= annee2 - 1
For j.l = annee1.l To annee2.l
longueurdumois.l = NombredeJoursduMois(2, j)
If longueurdumois = 28
Intervalle + 365
Else
Intervalle + 366
EndIf
Next j
EndIf
If annee1 < annee2
send.l = 12
Else
send = mois2 - 1
EndIf
If mois1 < send
For m = mois1 + 1 To send
Intervalle + NombredeJoursduMois(m, annne1)
Next m
EndIf
If mois2 > 1 And send = 12
For m = 1 To mois2 - 1
Intervalle + NombredeJoursduMois(m, annne2)
Next m
EndIf
If (mois1 = mois2) And (annee1 = annee2)
Intervalle = jour2 - jour1
Else
Intervalle + (NombredeJoursduMois(mois1, annee1) - jour1)
Intervalle + jour2
EndIf
ProcedureReturn Intervalle
EndProcedure
;today
y.l = Year(Date())
m.l = Month(Date())
d.l = Day(Date())
debug NombredeJoursentreDate(01, 03, 2004,d, m, y)

Posted: Fri Feb 24, 2006 12:49 pm
by Behnood
just look at the date command of date library in General Libraries section of purebasic manual.
it's completely covers what you need
Posted: Fri Feb 24, 2006 3:26 pm
by buzzqw
thanks ! i will look harder...
BHH
Posted: Fri Feb 24, 2006 3:30 pm
by Behnood
for finding days between 2 dates you can just use this:
date1=ParseDate("%yyyy/%mm/%dd", "2004/03/01")
date2=ParseDate("%yyyy/%mm/%dd", "2006/01/24")
date_bet=date2-date1
it will return seconds between date easily. so calculating days will be no matter then.
if you need precise answer (i mean counting intercalary years, different in month's days) you have to calculate it (may be there is a lib for that, i don't know) but if it isn't matter to have the precise answer, you can just use date() and it will return the date for seconds past from 1st jan 1970, so you know what you must do.
by the way it's 694 days between your dates and so it will be 1year 11months and 24days
Posted: Fri Feb 24, 2006 3:38 pm
by Dare2
Is it possible there is a base year you can subtract after converting to seconds, taking one from the other, and reconverting?
If not, the calc in basic code is pretty long winded requiring you work out leap years, false leap years, false false leap years and etc.
Posted: Fri Feb 24, 2006 3:42 pm
by Jan Vooijs
And to make it even easier:
Code: Select all
date1=ParseDate("%yyyy/%mm/%dd", "2004/03/01")
date2=ParseDate("%yyyy/%mm/%dd", "2006/01/24")
date_bet=date2-date1
Debug date_bet
Text.s = FormatDate( "%yyyy/%mm/%dd", Date_bet)
Debug Text.s
Gives as answer 1971/11/26 or 1 yr 11 months and 26 days...
Posted: Fri Feb 24, 2006 3:48 pm
by Dare2
hehe.
So 1970 is the base year!
Thanks.
Posted: Fri Feb 24, 2006 3:50 pm
by Behnood
as i know every software has its own base for date calculation. some take 1900 as a base (like ms products)
it's up to programmer and what he needs actually. cause dealing with date can be so hard. for example how i can find which day of week was 5000bc?!;-) there will be 2 answers, exact one and possible one. finding the exact one means you have to deal with calendar calculation in deep. but for a good approach i think counting seconds from the base is enough.
Posted: Fri Feb 24, 2006 3:52 pm
by Behnood
in my post, didn't i mention that the seconds will be count from 1st jan 1970?
wish to be helpful
Posted: Fri Feb 24, 2006 3:55 pm
by Dare2
lol. You did too! I missed it.

Posted: Fri Feb 24, 2006 4:07 pm
by Jan Vooijs
Dare2,
As memory serves I believe the 1970 calculations are a build-IN feature of MS windows since version 1.0 and YES they are exact to the second! Even leapyears and leap centuries are done correctly!! tested them myself...
There are even julian calculus in MS Windows (yes from 1/1/1900 til i believe the year 2800 (or 3500) i am not shure about the top date.
So fred has (had?) nothing to do with it just a call to windows and back to us the users...
Jan V.
Posted: Fri Feb 24, 2006 4:11 pm
by Dare2
Hi Jan,
That's interesting. Makes a change from DOS days when you had to roll your own!

Posted: Fri Feb 24, 2006 4:20 pm
by buzzqw
there is something wrong...
Code: Select all
date1=ParseDate("%yyyy/%mm/%dd", "2004/03/01")
date2=ParseDate("%yyyy/%mm/%dd", "2006/01/24")
date3=ParseDate("%yyyy/%mm/%dd", "1970/01/01")
date_bet=date2-date1-date3 ;<<<<< please note this
Debug date_bet
Text.s = FormatDate( "%yyyy/%mm/%dd", Date_bet)
Debug Text.s
in this way i would 01/11/26 BUT no i always get 1971 !
BHH
Posted: Fri Feb 24, 2006 4:20 pm
by Jan Vooijs
dare2,
Well good old Turbo Pascal had the Julian date functions for a very long time (That was under MS DOS)
Cheers
Jan V.
(