Asp.Net News
Free Tech Magazines
Asp.Net Web Hosting
Suggest Us
Link to Us
Feeds Subscription
Questions & Answers
Tips & Tricks
 Calculate the Number of Days, Months & Years with Asp.Net DateTime object
Posted by Moderator1 on  3/29/2009 1:53:18 AM 7509 Views
The Number of Days, Months and Years can be calculated in Asp.Net with the help of TimeSpan class. By using the Subtract Method of TimeSpan class, we can easily find out the interval between two DateTime objects. Follow the sample code below,

DateTime date1 = Convert.ToDateTime("2008/01/01");
DateTime date2 = Convert.ToDateTime("2008/12/31");

TimeSpan timespan = date2.Subtract(date1);

int NoofDays = timespan.Days;
double TotMonth = Math.Floor(NoofDays / 30.0);
double TotYear = Math.Floor(NoofDays / 365.0);

Response.Write("No. of Days : " + NoofDays.ToString());
Response.Write("No. of Months : " + TotMonth.ToString());
Response.Write("No. of Years : " + TotYear.ToString());

Post New Tips/Tricks View all Tips/Tricks

Featured Resources:
The PHP Anthology: 101 Essential Tips, Tricks & Hacks, 2nd Edition - Free 207 Page Preview!
A compilation of the best solutions provided to common PHP questions.
Many .NET teams are underperforming, is yours?
Explore how you can turn your .NET developer training program from a commonly ignored budget line-item into a strategic weapon in today's competitive software environment.
100% Free Subscription until Stock lost.View complete list.
Sponsored by