[quote=127142:@Thomas ROBISSON]I corrected my Excel method because it was wrong.
And I wrote it in RealStudio (I still developp with RealStudio to build in Xojo) :
DiffDateDaysMonthsYears.zip
Date are in the normal format, the French one which is Day / Month / Year
The Method
Dim DiffDay, DiffMonth, DiffYear, NbDaysInMonthB as Int16
Dim TampDate as Date
TampDate = New Date(DateB) ' Usefull only if DiffDay < 0 but I display it in the TextField
TampDate.Month = TampDate.Month + 1
TampDate.Day = 1
TampDate.TotalSeconds = TampDate.TotalSeconds - (24 *3600) ' 1 day previous
NbDaysInMonthB = TampDate.Day
TextFieldNbDaysInMont.Text = "There is " + str(NbDaysInMonthB) + " days in the month " + str(TampDate.Month) + " of year " + str(TampDate.Year)
If DateA.TotalSeconds < DateB.TotalSeconds Then
Beep
TextFieldDiffDate.Text = "Error, DateA must be greater than DateB (or equal) !"
Else
DiffDay = DateA.Day - DateB.Day
DiffMonth = DateA.Month - DateB.Month
If DiffDay < 0 Then DiffMonth = DiffMonth - 1
DiffYear = DateA.Year - DateB.Year
If DiffMonth < 0 Then DiffYear = DiffYear - 1
If DiffDay < 0 Then DiffDay = DiffDay + NbDaysInMonthB
If DiffMonth < 0 Then DiffMonth = DiffMonth + 12
TextFieldDiffDate.Text = str(DiffDay) + " days " + str(DiffMonth) + " months " + str(DiffYear) + " years"
End If
And I f I do the difference between 28 february and 1 march I have 1 day (2 if bissextil).
I learned something with Tim. When I wanted to copy a date, I use a variant to copy DateA in variant and the variant in dateB. Because DateB = DateA make the 2 dates linked. I never thinked about write DateB = New DateA .[/quote]
Thomas, I just tried your posted code, and several variables where not initialized. Maybe they are properties in your project.
Here is what I added :
Dim TampDate as new Date // lacked the 'new'
Dim DateB as new date(2013,2,20)
Dim DateA as new date(2014,2,5)
Apart form that, I see you count days until the end of the month. As Tim would note, must be a cultural difference (smile).
There is yet one difference with Christian’s TimeDifferenceMBS : you seem to include the day of the starting date, so you get 13 days where MBS finds 12, probably because he starts counting the day after.
In a way, his method uses an hour counting analogous to the calendar way of counting days : if the time on the starting date is 00:00 his method produces 13 days. If time has elapsed the day has started, so he counts the time until the end of the day, then starts counting days until the end of the month.
This whole discussion about what started as a seemingly simple problem is interesting since it shows how time measurement could be relative to the context.