Find Difference between 2 dates in Year, month, day, hour, minute, second and millisecond.
Posted by Viral Sarvaiya on May 3, 2013
After a long time i get time to post in my blog.
Few day ago i get very good function which finds difference between 2 dates.
Below is the class named “DateTimeSpan.cs”
using Microsoft.VisualBasic; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Diagnostics; public struct DateTimeSpan { private readonly int m_years; private readonly int m_months; private readonly int m_days; private readonly int m_hours; private readonly int m_minutes; private readonly int m_seconds; private readonly int m_milliseconds; public DateTimeSpan(int years, int months, int days, int hours, int minutes, int seconds, int milliseconds) { this.m_years = years; this.m_months = months; this.m_days = days; this.m_hours = hours; this.m_minutes = minutes; this.m_seconds = seconds; this.m_milliseconds = milliseconds; } public int Years { get { return m_years; } } public int Months { get { return m_months; } } public int Days { get { return m_days; } } public int Hours { get { return m_hours; } } public int Minutes { get { return m_minutes; } } public int Seconds { get { return m_seconds; } } public int Milliseconds { get { return m_milliseconds; } } private enum Phase { Years, Months, Days, Done } public static DateTimeSpan CompareDates(DateTime date1, DateTime date2) { if (date2 < date1) { dynamic sub = date1; date1 = date2; date2 = sub; } DateTime current = date1; int years = 0; int months = 0; int days = 0; Phase phase__1 = Phase.Years; DateTimeSpan span = new DateTimeSpan(); while (phase__1 != Phase.Done) { switch (phase__1) { case Phase.Years: if (current.AddYears(years + 1) > date2) { phase__1 = Phase.Months; current = current.AddYears(years); } else { years += 1; } break; // TODO: might not be correct. Was : Exit Select break; case Phase.Months: if (current.AddMonths(months + 1) > date2) { phase__1 = Phase.Days; current = current.AddMonths(months); } else { months += 1; } break; // TODO: might not be correct. Was : Exit Select break; case Phase.Days: if (current.AddDays(days + 1) > date2) { current = current.AddDays(days); dynamic timespan = date2 - current; span = new DateTimeSpan(years, months, days, timespan.Hours, timespan.Minutes, timespan.Seconds, timespan.Milliseconds); phase__1 = Phase.Done; } else { days += 1; } break; // TODO: might not be correct. Was : Exit Select break; } } return span; } }
Now in default.aspx page i am using this structure to get difference between 2 dates as below.
protected void Page_Load(object sender, EventArgs e) { DateTimeSpan datetimespan = new DateTimeSpan(); // Create object of constructer to get difference. string date1 = "3-May-2013 9:26:10.011 AM"; //Date 1 string date2 = "4-June-2014 6:50:20.136 PM"; // date2 datetimespan = DateTimeSpan.CompareDates(Convert.ToDateTime(date1), Convert.ToDateTime(date2)); //Call static function of DateTimeSpan structure. which return difference of 2 dates. Response.Write("Date 1 : " + date1 + " <br>"); Response.Write("Date 2 : " + date2 + " <br><br>"); Response.Write("Years : " + datetimespan.Years + "<br>"); Response.Write("Months : " + datetimespan.Months + "<br>"); Response.Write("Days : " + datetimespan.Days + "<br>"); Response.Write("Hours : " + datetimespan.Hours + "<br>"); Response.Write("Minutes : " + datetimespan.Minutes + "<br>"); Response.Write("Seconds : " + datetimespan.Seconds + "<br>"); Response.Write("Milliseconds : " + datetimespan.Milliseconds + "<br>"); }
This will give difference in all possible ways as below output.
Hope this will helps you.
Thanks.
Advertisements
Leave a Reply