C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Training   ASP.NET Web Hosting    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

Play Silverlight Games or Submit your Silverlight applications and earn 90% AdSense revenue.

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Resources » Articles » .NET Framework »

DateDiff in c#


Posted Date: 12 Sep 2005    Resource Type: Articles    Category: .NET Framework
Author: MaheshMember Level: Silver    
Rating: Points: 10



The status of this resource is Rejected.







Responses

Author: Rajeev     12 Nov 2007Member Level: Silver   Points : 0

using System;


namespace ReflectionIT.System {

public enum DateInterval {
Year,
Month,
Weekday,
Day,
Hour,
Minute,
Second
}

public class DateTimeUtil {

public static long DateDiff(DateInterval interval, DateTime date1, DateTime date2) {

TimeSpan ts = ts = date2 - date1;

switch (interval) {
case DateInterval.Year:
return date2.Year - date1.Year;
case DateInterval.Month:
return (date2.Month - date1.Month) + (12 * (date2.Year - date1.Year));
case DateInterval.Weekday:
return Fix(ts.TotalDays) / 7;
case DateInterval.Day:
return Fix(ts.TotalDays);
case DateInterval.Hour:
return Fix(ts.TotalHours);
case DateInterval.Minute:
return Fix(ts.TotalMinutes);
default:
return Fix(ts.TotalSeconds);
}
}

private static long Fix(double Number) {
if (Number >= 0) {
return (long)Math.Floor(Number);
}
return (long)Math.Ceiling(Number);
}
}
}





Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Creating Dynamic Thumbnail Images through ASP.NET
Previous Resource: Understanding Delegates in C#
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use