Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Count months between two dates
Message
 
 
À
11/08/2008 03:38:16
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 9 SP2
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
Visual FoxPro
Divers
Thread ID:
01337391
Message ID:
01338007
Vues:
35
>>I see it's a very interesting article, but at the moment I don't have time to translate. I'll take it at home and translate on Sunday, OK?
>Yes, of course.

Hi Serg,

Here is the translation, it's not very good, but I found it difficult for me to be a translator.

How can we get the difference between two dates in a format of years, months and days?

The answer is - in a general case this question doesn’t have a solution.

Don’t try to contradict this statement immediately showing your solution or a link to a known solution. Read this article till the end first.

The problem here is that there is no unambiguous definition of what is a month and what is a year.
As a common rule, we assume here a calendar month and a calendar year. But then again, the calendar month is not an absolute value, but relative.

The calendar month is not a fixed number of days. It is a number of days passed from a certain date.

To understand the problem better let’s consider the following example.

Say, we need to calculate difference between January 30th and April 2nd of the year 2008.

If we need to find the difference in days, we just subtract one date from another.

?{^2008-04-02} - {^2008-01-30} && 63 days

But how can we determine the number of months?

There are several possible solutions:

1. Let’s add to the starting date a month till result would not be greater than the ending date.
Here by adding a month we imply an operation where the day of the date remains the same and the month is incremented by 1. If the month doesn’t have this day in it, we use the last date of the next month. This is how VFP GOMONTH() function works.
It means, that if we add one month to January 30th we would get February 29th (the 2008 year is a leap year) since there is no 30th day in February.

Then we add one more month to February 29th and get March 29th. We don’t need to add one more month since the result would be greater than April 2nd. Between March, 29 and April, 2 there are 4 days. So, as a result we get 2 months and 4 days.

If this would not be a leap year, we would get 2 months and 5 days.

Also, let’s note, that for January 29, January 30 and January 31 we get the same result!

It’s because the adding of one month will always give us February 29. We have a paradoxical situation – the number of days in between is different, but the number of months + days remains the same!


2. This time the algorithm is similar to the algorithm from the first solution, but let’s add the number of months to get closer to the original date. In other words, let’s add 2 months to January 30 to get March 30. Since there are 3 days between March 30 and April 2 we get 2 months and 3 days. Let’s also note, that if the end date would be March 2 and not the April 2, we would get the same result as in the first approach.


3. Let’s define the month as a calendar month, e.g. February is an interval between February 1st and February 29th , March - from March 1st till March 30th.
In this case we have 2 full months – February and March and the remainders from January and April. So, the result is 2 months and 3 days.
In this case we got lucky, since 3 days are not enough to form a month. But what we do in case of 40 days as in January 10 and April 20? There is a question of how many days we need to take from this amount to get one more full month. Would we take days from January (the month of the starting date) or April (the month of the ending date)?


4. Let’s assume that all months have the same number of days that we can calculate by this formula:
(365+365+365+366)/4/12 = 30.4375
Then the number of days is simply a division of total number of days to this constant rounded. Using this formula we get 2 months and 1 day.

There could be other solutions to this problem as well.

As we see, the result depends on how we define a month. To be precise, how can we define a month in a given dates period. Also we could not say, that one algorithm is correct and the others are not. All algorithms are correct based on the assumptions we take.

In other words, the question of calculating the number of months between two dates becomes not a coding question but rather the question of choosing a certain methodology (ideology) of calculation.
Once we agree on the methodology, coding it becomes a simple task.

The choice of a different methodology usually depends on the specific problem we need to solve:
do we need to congratulate someone with his anniversary (not a problem to do it multiple times ), calculate the length of service or show the statistics of births in a hospital.

We can also note, that the same difficulties arouse for other calendar periods such as week, quarter or year. In other words, before writing code we need to establish some rules that would govern the definition of these intervals.

You can find some solutions on this or other sites. But you should always be aware that these methods follow a certain methodology of calculation that is not always easy to understand from the code. That’s why the result of calculation may be different from the expected result. You have to thoroughly test the code to determine if it would satisfy your requirement.








If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform