Just starting out? Need help? Post your questions and find answers here.
buzzqw
Enthusiast
Posts: 116 Joined: Sat Aug 27, 2005 10:13 pm
Location: Italy
Contact:
Post
by buzzqw » Fri Feb 24, 2006 11:39 am
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
flaith
Enthusiast
Posts: 704 Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:
Post
by flaith » Fri Feb 24, 2006 12:42 pm
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)
“Fear is a reaction. Courage is a decision.” - WC
Behnood
User
Posts: 72 Joined: Fri Apr 25, 2003 10:07 pm
Location: Australia
Contact:
Post
by Behnood » Fri Feb 24, 2006 12:49 pm
just look at the date command of date library in General Libraries section of purebasic manual.
it's completely covers what you need
buzzqw
Enthusiast
Posts: 116 Joined: Sat Aug 27, 2005 10:13 pm
Location: Italy
Contact:
Post
by buzzqw » Fri Feb 24, 2006 3:26 pm
thanks ! i will look harder...
BHH
Behnood
User
Posts: 72 Joined: Fri Apr 25, 2003 10:07 pm
Location: Australia
Contact:
Post
by Behnood » Fri Feb 24, 2006 3:30 pm
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
Dare2
Moderator
Posts: 3321 Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land
Post
by Dare2 » Fri Feb 24, 2006 3:38 pm
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.
@}--`--,-- A rose by any other name ..
Jan Vooijs
Enthusiast
Posts: 196 Joined: Tue Sep 30, 2003 4:32 pm
Location: The Netherlands
Post
by Jan Vooijs » Fri Feb 24, 2006 3:42 pm
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...
Life goes to Fast, Enjoy!!
PB 4 is to good to be true, wake up man it is NOT a dream THIS is a reality!!!
AMD Athlon on 1.75G, 1Gb ram, 160Gb HD, NVidia FX5200, NEC ND-3500AG DVD+RW and CD+RW, in a Qbic EO3702A and Win XP Pro SP2 (registered)
Dare2
Moderator
Posts: 3321 Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land
Post
by Dare2 » Fri Feb 24, 2006 3:48 pm
hehe.
So 1970 is the base year!
Thanks.
@}--`--,-- A rose by any other name ..
Behnood
User
Posts: 72 Joined: Fri Apr 25, 2003 10:07 pm
Location: Australia
Contact:
Post
by Behnood » Fri Feb 24, 2006 3:50 pm
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.
Behnood
User
Posts: 72 Joined: Fri Apr 25, 2003 10:07 pm
Location: Australia
Contact:
Post
by Behnood » Fri Feb 24, 2006 3:52 pm
in my post, didn't i mention that the seconds will be count from 1st jan 1970?
wish to be helpful
Dare2
Moderator
Posts: 3321 Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land
Post
by Dare2 » Fri Feb 24, 2006 3:55 pm
lol. You did too! I missed it.
@}--`--,-- A rose by any other name ..
Jan Vooijs
Enthusiast
Posts: 196 Joined: Tue Sep 30, 2003 4:32 pm
Location: The Netherlands
Post
by Jan Vooijs » Fri Feb 24, 2006 4:07 pm
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.
Life goes to Fast, Enjoy!!
PB 4 is to good to be true, wake up man it is NOT a dream THIS is a reality!!!
AMD Athlon on 1.75G, 1Gb ram, 160Gb HD, NVidia FX5200, NEC ND-3500AG DVD+RW and CD+RW, in a Qbic EO3702A and Win XP Pro SP2 (registered)
Dare2
Moderator
Posts: 3321 Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land
Post
by Dare2 » Fri Feb 24, 2006 4:11 pm
Hi Jan,
That's interesting. Makes a change from DOS days when you had to roll your own!
@}--`--,-- A rose by any other name ..
buzzqw
Enthusiast
Posts: 116 Joined: Sat Aug 27, 2005 10:13 pm
Location: Italy
Contact:
Post
by buzzqw » Fri Feb 24, 2006 4:20 pm
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
Jan Vooijs
Enthusiast
Posts: 196 Joined: Tue Sep 30, 2003 4:32 pm
Location: The Netherlands
Post
by Jan Vooijs » Fri Feb 24, 2006 4:20 pm
dare2,
Well good old Turbo Pascal had the Julian date functions for a very long time (That was under MS DOS)
Cheers
Jan V.
(
Life goes to Fast, Enjoy!!
PB 4 is to good to be true, wake up man it is NOT a dream THIS is a reality!!!
AMD Athlon on 1.75G, 1Gb ram, 160Gb HD, NVidia FX5200, NEC ND-3500AG DVD+RW and CD+RW, in a Qbic EO3702A and Win XP Pro SP2 (registered)