Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2007

Tip: Looking for answers? Try searching our database.

How to calculate the difference between 2 dates in days, months and years

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alexander Vasilevsky - 20 Dec 2007 19:13 GMT
How to calculate the difference between 2 dates in days, months and years?

http://www.alvas.net - Audio tools for C# and VB.Net developers + Christmas
discount
sloan - 20 Dec 2007 20:08 GMT
I'd start with the TimeSpan object.

http://msdn2.microsoft.com/en-us/library/system.timespan.totaldays.aspx

> How to calculate the difference between 2 dates in days, months and years?
>
> http://www.alvas.net - Audio tools for C# and VB.Net developers +
> Christmas discount
jlnm - 20 Dec 2007 20:43 GMT
> I'd start with the TimeSpan object.
>
[quoted text clipped - 6 lines]
>
> - Show quoted text -

Try This

System.DateTime FirstDate
System.DateTime SecondDate
System.DateTime OriginalDate
int year,month,day,monthDays;
year=month=day=monthDays=0;

OriginalDate=FirstDate;
monthDays=System.DateTime.DaysInMonth(OriginalDate.Year,OriginalDate.Month);

year=FirstDate.year-SecondDate.Year
if(SecondDate.DayOfYear<FirstDate.DayOfYear)
{
    year--;
}

FirstDate=new
System.DateTime(SecondDate.Year,FirstDate.Month,FirstDate.Day);
month=Math.Abs(FirstDate.Month-((SecondDate.Month<FirstDate.Month) ?
12+FirstDate.Month : FirstDate.Month));

if(SecondDate.Day<FirstDate.Day)
{
    month--;
    day=monthDays-FirstDate.Day-SecondDate.Day;
}
else
{
    day=SecondDate.Day-FirstDate.Day;
}
jlnm - 20 Dec 2007 20:55 GMT
> > I'd start with the TimeSpan object.
>
[quoted text clipped - 48 lines]
>
> - Show quoted text -

Sorry:::::
System.DateTime FirstDate
System.DateTime SecondDate
int year,month,day,monthDays;
year=month=day=monthDays=0;

monthDays=System.DateTime.DaysInMonth(FirstDate.Year,FirstDate.Month);

year=FirstDate.year-SecondDate.Year
if(SecondDate.DayOfYear<FirstDate.DayOfYear)
{
    year--;
}

month=Math.Abs(FirstDate.Month-((SecondDate.Month<FirstDate.Month) ?
12+FirstDate.Month : FirstDate.Month));

if(SecondDate.Day<FirstDate.Day)
{
    month--;
    day=monthDays-FirstDate.Day+SecondDate.Day;
}
else
{
    day=SecondDate.Day-FirstDate.Day;
}
jlnm - 21 Dec 2007 12:15 GMT
> > > I'd start with the TimeSpan object.
>
[quoted text clipped - 81 lines]
>
> - Show quoted text -
I fired that off, yesterday.  You'll have to tweak it.  But that is
the idea!

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.