By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,068 Members | 1,734 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,068 IT Pros & Developers. It's quick & easy.

Years, Months and Days between tow dates

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Juan,

Try subtracting two DateTime variables and you'll end up with TimeSpan
structure.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"Juan" <ju*********@hotmail.com> wrote in message
news:OB**************@TK2MSFTNGP10.phx.gbl...
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

Nov 16 '05 #2

P: n/a
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*R SS*

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...aspx?FeedId=41

*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*R SS*

"Juan" <ju*********@hotmail.com> wrote in message
news:OB**************@TK2MSFTNGP10.phx.gbl...
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

Nov 16 '05 #3

P: n/a
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*R SS*
::
:: 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...aspx?FeedId=41
::
:: *RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*R SS*
::
::
::
::
::
:: "Juan" <ju*********@hotmail.com> wrote in message
:: news:OB**************@TK2MSFTNGP10.phx.gbl...
::: 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
Nov 16 '05 #4

P: n/a

"Juan" <ju*********@hotmail.com> wrote in message
news:OB**************@TK2MSFTNGP10.phx.gbl...
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
Nov 16 '05 #5

P: n/a
Juan wrote:
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.
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*R SS*
::
:: 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...aspx?FeedId=41
::
:: *RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*R SS*
::
::
::
::
::
:: "Juan" <ju*********@hotmail.com> wrote in message
:: news:OB**************@TK2MSFTNGP10.phx.gbl...
::: 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

--
mikeb
Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.