In this tutorial, you will learn how to calculate number of days, months and years between two given dates in asp.net using c#. Asp.net has built-in class TimeSpan. Using TimeSpan class you can calculate no of days, hours, minutes, seconds and even milliseconds but if you want to calculate total no of months and years between two given dates then TimeSpan class cannot help you. So that's why the focus of this article is to calculate exact number of days, months and years between two given dates using custom class given below. Let's have a look over it.
Calculate number of years, months and days between two given dates in asp.net using c#
Calculate-days-months-years.aspx.csprotected void Page_Load(object sender, EventArgs e) { DateTime dt1 = Convert.ToDateTime("02/01/2011"); DateTime dt2 = Convert.ToDateTime("11/04/2010"); //Initializing object and then calling constructor CalculateDateDifference dateDiff = new CalculateDateDifference(dt1,dt2); int totalMonths = dateDiff.Months; int totalDays = dateDiff.Days; int totalYears= dateDiff.Years; } //Class that will calculate the number of days, months and years between two given dates. public class CalculateDateDifference { /// <summary> /// defining Number of days in month; index 0 represents january and 11 represents December /// february contain either 28 or 29 days, so here its value is -1 /// which will be calculate later. /// </summary> private int[] monthDay = new int[12] { 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; /// <summary> /// contain from date /// </summary> private DateTime fromDate; /// <summary> /// contain To Date /// </summary> private DateTime toDate; /// <summary> /// these three variable of integer type for output representation.. /// </summary> private int year; private int month; private int day; //Public type Constructor public CalculateDateDifference(DateTime d1, DateTime d2) { int increment; //To Date must be greater if (d1 > d2) { this.fromDate = d2; this.toDate = d1; } else { this.fromDate = d1; this.toDate = d2; } /// /// Day Calculation /// increment = 0; if (this.fromDate.Day > this.toDate.Day) { increment = this.monthDay[this.fromDate.Month - 1]; } /// if it is february month /// if it's to day is less then from day if (increment == -1) { if (DateTime.IsLeapYear(this.fromDate.Year)) { // leap year february contain 29 days increment = 29; } else { increment = 28; } } if (increment != 0) { day = (this.toDate.Day + increment) - this.fromDate.Day; increment = 1; } else { day = this.toDate.Day - this.fromDate.Day; } /// ///month calculation /// if ((this.fromDate.Month + increment) > this.toDate.Month) { this.month = (this.toDate.Month + 12) - (this.fromDate.Month + increment); increment = 1; } else { this.month = (this.toDate.Month) - (this.fromDate.Month + increment); increment = 0; } /// /// year calculation /// this.year = this.toDate.Year - (this.fromDate.Year + increment); } public override string ToString() { //return base.ToString(); return this.year + " Year(s), " + this.month + " month(s), " + this.day + " day(s)"; } public int Years { get { return this.year; } } public int Months { get { return this.month; } } public int Days { get { return this.day; } } }Now totalDays variable will have total number of days between these given dates, likewise totalMonthstotalYears variable will have total number of years between two given dates. So this is the way to calculate number of years, months and days between two given dates in asp.net using c#. variable will have total number of months and
I hope it will be a great helpful for you.
Happy Coding, Keep Coding.