473,378 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Years, Months and Days between tow dates

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
5 8796
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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bambero | last post by:
Hello all Problem like in subject. There is no problem when I want to count days between two dates. Problem is when I want to count years becouse of leap years. For ex. between 2002-11-19...
3
by: MMFBprez | last post by:
I am trying to compute storage charges by getting the number of months between dates and multiplying it by a rate. I cannot get a correct number of months if the date is greater than a year ago. ...
6
by: carl.barrett | last post by:
Hi, I have a continuous form based on a query ( I will also be creating a report based on the same query). There are 2 fields: Date Obtained and Date Of Expiry I want a further 3 columns...
1
by: ann | last post by:
is there someway to disable the Other Months day(the previous and next months days) s from appearing? If so, can you let me know how? Thanks so much for your help!
14
by: jpr | last post by:
Friends, I have a form with four fields, date1, date2, date3 and date4. All these have all a mm/dd/yyyy format and have their source to a table. I need to add an unbound control (I will name...
8
by: Jose | last post by:
Exists a function that determined between two dates the years,months and days? Thanks a lot.
23
by: thebjorn | last post by:
For the purpose of finding someone's age I was looking for a way to find how the difference in years between two dates, so I could do something like: age = (date.today() - born).year but that...
15
karthickkuchanur
by: karthickkuchanur | last post by:
Hai sir, i assigned to calculate the age from current date ,i have the date of birth of employee from that i have to calculate the age dynamically, i refer to google it was confusing...
2
by: johanneguaybenoit | last post by:
Hi I would like to know how to create a function or module to calculate the age of a person in years months days. and hours if feasable But I really nead to know in years months days. I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.