Getting the date difference in years months days.

[quote=127444:@John Fatte]An observation after reading this thread… this is a good example why some XOJO lurkers will opt to use something else.

Comments about someone’s suggestions should be positive and constructive rather than negative and demeaning.[/quote]

Changing development tool because one individual has bad manners is not logical. This forum has a great feature : click the name of the bad person, click ‘Ignore’,and all the posts from that particular individual will be suppressed from your view.

Pitiful name calling incident aside, I think this thread is on a contrary an example of why one would appreciate Xojo : such a thorough and high level exchange would not take place on many forums.

And unfortunately, there are forums out there where people are quite often an in larger number very nasty, like StackOverflow.com

Would this also not do the trick :

[code] // This function calculates the amount of days between
// datLowDate and datHighDate. The amount of days
// are returned as an integer

Dim intDays As Integer

intDays = (datHighDate.TotalSeconds - datLowDate.TotalSeconds) / 86400

Return intDays[/code]

datHighDate and datLowDate are type Date and placed inside the function parameters.

This function returns the difference between two dates in days.

This function will calculate the difference in weeks between 2 dates :

[code]// This function calculates the amount of weeks between
// datLowDate and datHighDate. The amount of weeks
// are returned as an integer

Dim intWeeks As Integer

intWeeks = (datHighDate.TotalSeconds - datLowDate.TotalSeconds) / 604800

Return intWeeks[/code]

I use both functions for years now and they always did the work without any error. Simple and easy. Why make it more difficult than it is?

Hope this helps.

I can no longer edit my former post but I forgot the month difference. So here it is :

[code]// This function calculates the amount of months between
// datLowDate and datHighDate. The amount of months
// are returned as an integer

Dim intMonths As Integer

intMonths = (datHighDate.TotalSeconds - datLowDate.TotalSeconds) / 2592000

Return intMonths[/code]

Have a nice day

Chris

The number of days and number of weeks are most accurate, since the length of these units is fixed.

Now the big issue comes with the length of the month. You divide the difference in Totalseconds by 2592000, which is a 30 days month. It works fine to estimate the number of months apparently.

But the length of months is variable. On a non-leap year, the average length of the month is not 30 days. It is 30.416666666. Let us say 30.4167. For estimating the number of months it does not seem to pose problem, but to estimate the number of remaining days, which took quite a bit of this thread, it becomes an issue.

intDays-(intMonths*30.4167) produces 15.4163, which could be used with ceil to give 16 days, identical to Hamish and Tim’s method for 2013/2/20 - 2014/2/5. But this may require more testing with edge cases.

I like your approach for the number of weeks and months and the total number of days.

Thank you Michel for your explanation. I learned something new today. This is indeed a very interesting subject.

Have a nice day.

“Ask an accountant to calculate the same and I bet you he will proceed by calendar days. Let alone not to pay people on extra virtual days created by the method. Business usually considers months start on the first, not in the middle.”

Depends on the purpose. Each country has its own legal rules for how to compute months/days.

http://en.wikipedia.org/wiki/360-day_calendar

[quote=127536:@J Andrew Lipscomb]Depends on the purpose. Each country has its own legal rules for how to compute months/days.

http://en.wikipedia.org/wiki/360-day_calendar
[/quote]

Interestingly, this summarizes the cultural differences. As J Andrew Lipscomb shows perfectly well, the 30 days month conduces to valid results in estimating the number of months, so it is probably not necessary to bother with a precise counting on days per month.

The main point this thread has shown is the very big difference between the US and Europe when counting days is concerned.

As you say, depends on the purpose. The accountant preparing payroll, or the bank calculating interest charges will do that on actual days.

Even within the US, there is disagreement about Daylight Savings Time… just ask Arizona :slight_smile:

Also, another difference I noticed about dates when it comes to the US and Europe has to do with how dates are being displayed. Example:
US: MM/DD/YYYY
UK: DD/MM/YYYY

The only time I am aware in the US where we refer date as DD/MM is the 4th of July… Then you have the topic of what day starts a week day. Is it Sunday or Monday?

As Norm said… Dates… soo much fun :stuck_out_tongue:

:smiley:

Start a week on a Sunday…pfff :wink:

Don’t get me started about TIME (never mind dates) and daylight savings time :slight_smile:

DST is a political gadget. Unfortunately, politicians seldom go by science…

For the date, I prefer ours (French), DD is the lower, then MM middle, the YY biggest. More logic than MM DD YY :slight_smile: .
Anyway, when I save file on my Mac with date in the name, I write MyFile YYYY-MM and eventually I write -DD .

YYYYMMDD makes the most sense for computer use
It naturally sorts in the correct order and its easy to parse
Oh and it was naturally also Y2K (and Y3K and Y4k … up to Y10K compliant)
Yes - been there done that glad the accounting system used that form and only had to change 4 lines of code (after auditing about 80,000 lines)

Horrible for human consumption though :stuck_out_tongue:

I think any of us don’t care about this case :smiley: