Years, Months and Days between tow dates

  • Thread starter Thread starter Juan
  • Start date Start date
J

Juan

Hi everyone,

is there a function that calculates the exact amount of Years, Months, and
Days between two days?

TimeDiff is not good enough, since it calculates, for example:

Date 1: Dec. 31st 2003
Date 2: Jan 1st 2004

The real difference is 0 years, 0 months, 1 day. DateDiff returns 1 year, 1
month, 1 day.

Is there a funcion, or rutine, or does anyone know a good way to achieve the
desired result?

Thanks, from Spain
 
Hi Juan,

Try subtracting two DateTime variables and you'll end up with TimeSpan
structure.
 
Subtract one datetime from another to return a TimeSpan. You can get the
time accurate to the millisecond.

--
Bob Powell [MVP]
Visual C#, System.Drawing

All you ever wanted to know about ListView custom drawing is in Well Formed.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*

The GDI+ FAQ: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://royo.is-a-geek.com/siteFeeder/GetFeed.aspx?FeedId=41

*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*
 
But I can´t get the numbers of years and mohts transcurred from a TimeSpan,
only the number of days, and that doesn´t solve the Dec 31st 2001 to Jan 1st
2004 problem. I will find out the number of days, but not the number of
months or years.

Bob Powell [MVP] wrote:
:: Subtract one datetime from another to return a TimeSpan. You can get
:: the time accurate to the millisecond.
::
:: --
:: Bob Powell [MVP]
:: Visual C#, System.Drawing
::
:: All you ever wanted to know about ListView custom drawing is in Well
:: Formed. http://www.bobpowell.net/currentissue.htm
::
:: Answer those GDI+ questions with the GDI+ FAQ
:: http://www.bobpowell.net/gdiplus_faq.htm
::
:: *RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*
::
:: The GDI+ FAQ: http://www.bobpowell.net/faqfeed.xml
:: Windows Forms Tips and Tricks:
:: http://www.bobpowell.net/tipstricks.xml Bob's Blog:
:: http://royo.is-a-geek.com/siteFeeder/GetFeed.aspx?FeedId=41
::
:: *RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*
::
::
::
::
::
:: ::: Hi everyone,
:::
::: is there a function that calculates the exact amount of Years,
::: Months, and Days between two days?
:::
::: TimeDiff is not good enough, since it calculates, for example:
:::
::: Date 1: Dec. 31st 2003
::: Date 2: Jan 1st 2004
:::
::: The real difference is 0 years, 0 months, 1 day. DateDiff returns 1
::: year, 1 month, 1 day.
:::
::: Is there a funcion, or rutine, or does anyone know a good way to
::: achieve the desired result?
:::
::: Thanks, from Spain
 
Juan said:
Hi everyone,

is there a function that calculates the exact amount of Years, Months, and
Days between two days?

TimeDiff is not good enough, since it calculates, for example:

Date 1: Dec. 31st 2003
Date 2: Jan 1st 2004

The real difference is 0 years, 0 months, 1 day. DateDiff returns 1 year, 1
month, 1 day.

Is there a funcion, or rutine, or does anyone know a good way to achieve the
desired result?

You need to think a little more about whether your "desired result" makes
sense.

Months and Years are not fixed Timespans. Given 2 DateTime's the rule for
DateDiff is, and has to be, that the difference in years between the 2 dates
is the number of times you cross the "year boundary" (Jan 1st 12:00 am),
when traveling from the first date to the second.

Other rules are sometimes used, but they have their own difficulties.

For instance what is the difference in months between

Date 1: Jan. 31st 2003
Date 2: Feb. 28st 2003

?
The interval is 28 days long.

January has 31 days, so is the differnece is 0?
February has 28 days, so is the differnece is 1?
Or is a month defined as a 30 day timespan, so is the difference is 0?


What about
Date 1: Jan. 31st 2004
Date 2: Feb. 28st 2004
?
January has 31 days, and February has 29 days so is the difference is 0?


And what about
Date 1: Feb. 28st 2003
Date 2: Jan. 31st 2003
?
is DateDiff(d1,d2) = -DateDiff(d2,d1)?

David
 
Juan said:
But I can´t get the numbers of years and mohts transcurred from a TimeSpan,
only the number of days, and that doesn´t solve the Dec 31st 2001 to Jan 1st
2004 problem. I will find out the number of days, but not the number of
months or years.

you can use VB.NET's DateDiff() function. Either wrap it in a class
that you can easily call from C# or call it directly by referencing the
Microsoft.VisualBasic assembly - it's in the
Microsoft.VisualBasic.DateAndTime class.
 
Back
Top