Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
| Search | Today's Posts | Mark Forums Read
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
 
Old October 6th, 2006, 12:38 PM
Authorized User
 
Join Date: Aug 2004
Location: memphis, tn, .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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



 
Old October 6th, 2006, 12:51 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

I know how to get the exact amount of *days* between 2 dates using DateDiff in SQL, but that really doesn't help you. (You can also do the same thing with a timespan but this gives you days as well, not years)

These articles might help you:
http://www.codeguru.com/vb/gen/vb_mi...cle.php/c6779/
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=183
http://articles.techrepublic.com.com...1-5760752.html

--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.
 
Old October 7th, 2006, 12:33 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: United Kingdom
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old October 7th, 2006, 09:59 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

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.
 
Old October 9th, 2006, 12:58 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: United Kingdom
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanx buddy for the appreciation

Regards
Mike
 
Old October 13th, 2006, 12:36 PM
Authorized User
 
Join Date: Aug 2004
Location: memphis, tn, .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old October 14th, 2006, 12:14 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: United Kingdom
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

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




Similar Threads
Thread Thread Starter Forum Replies Last Post
DateDiff excluding weekend days ghall202 Access VBA 1 March 3rd, 2008 09:48 AM
date conversion in years, months and days naveed77 Beginning VB 6 0 January 2nd, 2008 01:18 AM
total days calculation exclude public holiday kumiko Classic ASP Basics 6 June 1st, 2007 01:00 AM
Years to Months Conversion mahajanvit ASP.NET 2.0 Basics 2 September 25th, 2006 11:43 PM
leap years & cal_days_in_month richard.york Pro PHP 4 December 27th, 2003 02:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.