 |
| ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

October 6th, 2006, 12:38 PM
|
|
Authorized User
|
|
Join Date: Aug 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
DateDiff calculation for Years, Months & Days
All:
Does anyone know how to calculate the lenght of time between two date in the exact number of years, months and days? I have written a Sub that breaks length of service time into days from an employee's hire date until Now. I then went through normal division,
multiplication and Mod logic to break it into Years, Months and Days based on a 365-day year and 30-day months. However, over a long period of time you lose or gain days because not every year is 365 days and not every month is 30 days. Perhaps I'm over explaining this but you see what I mean. Has anyone else had this experience?
Has anyone written or encountered a solution? Am I possibly missing or misinterpreting a .Net built-in function that is already out there? I'm using ASP.Net 1.1 and visual Studio 2003. Your help would be greatly appreciated.
Demivolt
|
|

October 7th, 2006, 12:33 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2004
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Hi
You can use the System.TimeSpan class to acheive this. Just try this out, this should work for you:
DateTime dt1 = new DateTime();
dt1 = Convert.ToDateTime( "2006/10/07");
DateTime dt2 = new DateTime();
dt2 = Convert.ToDateTime( "2004/07/17");
TimeSpan tSpan = dt1 - dt2;
double iYears, iMonths, iDays;
iYears = Convert.ToDouble(tSpan.TotalDays) / 365;
iMonths = iYears * 12;
iDays = Convert.ToDouble(tSpan.TotalDays);
Response.Write("Years=" + iYears + "-----" + "Months=" + iMonths + "-----" + "Days=" + iDays);
Regards
Mike
|
|

October 7th, 2006, 09:59 AM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
Mike,
Thats brilliant!
--Stole this from a moderator
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
|
|

October 9th, 2006, 12:58 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2004
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Thanx buddy for the appreciation
Regards
Mike
|
|

October 13th, 2006, 12:36 PM
|
|
Authorized User
|
|
Join Date: Aug 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Mike,
Thanks for posting the code. I should have stated that I am working in visual basic. When I try your code, Visual Studio gives me a syntaxt error prohibiting
subtacting a date from a date. I have dt1 and dt2 dimmed as DateTime and tSpan dimmed as TimeSpan. Any ideas from anyone?
Thanks, Demivolt
|
|

October 14th, 2006, 12:14 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2004
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Hey Demivolt
Then its more simpler, just check the code below:
Dim dt1 As DateTime = New DateTime
dt1 = Convert.ToDateTime("2004/10/07")
Dim dt2 As DateTime = New DateTime
dt2 = Convert.ToDateTime("2006/07/17")
Dim iYears As Integer, iMonths As Integer, iDays As Integer
iYears = DateDiff(DateInterval.Year, dt1, dt2)
iMonths = DateDiff(DateInterval.Month, dt1, dt2)
iDays = DateDiff(DateInterval.Day, dt1, dt2)
Response.Write("Years=" & iYears & "-----" + "Months=" & iMonths & "-----" & "Days=" & iDays)
Regards
Mike
|
|
 |