adsense

Monday, May 10, 2010

Calculating Date Difference in C#

The direct and simplest way to achieve this is by using the Microsoft.VisualBasic namespace and invoking the DateDiff method.

E.g.
long nYears = Microsoft.VisualBasic.DateAndTime.DateDiff(Microsoft.VisualBasic.DateInterval.Year, dt, dtNow, Microsoft.VisualBasic.FirstDayOfWeek.System, Microsoft.VisualBasic.FirstWeekOfYear.System);

There are some arguments over using this. Have look at the following link.
http://www.c-sharpcorner.com/UploadFile/DipalChoksi/DateDiff_CS_DC09132006172429PM/DateDiff_CS_DC.aspx


Other option is to write your own class. Have a look at following posting
http://www.aspcode.net/C-Datediff.aspx


Cheers,
Happy coding
samitha

No comments:

Post a Comment