Here I have created sample that accepts from date and to date and on submit shows the message like "a minute ago", "an hour ago", "Three weeks ago", etc...
HTML
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>
<div>
Enter From Date :
<asp:TextBox ID="txtFromDate" runat="server" />
<cc1:CalendarExtender runat="server" TargetControlID="txtFromDate" PopupPosition="Right">
</cc1:CalendarExtender>
</div>
<br />
<div>
Enter To Date :
<asp:TextBox ID="txtToDate" runat="server" />
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtToDate"
PopupPosition="Right">
</cc1:CalendarExtender>
</div>
<br />
<div>
<asp:Button ID="btnGetAccurateDifference" Text="Get Accurate Difference" runat="server"
OnClick="btnGetAccurateDifference_Click" />
<br />
<br />
<asp:Label ID="lblMessage" runat="server" />
</div>
C#
protected void btnGetAccurateDifference_Click(object sender, EventArgs e)
{
this.lblMessage.Text = this.GetAccurateTimeDiff(Convert.ToDateTime(this.txtFromDate.Text.Trim()), Convert.ToDateTime(this.txtToDate.Text.Trim()));
}
private string GetAccurateTimeDiff(DateTime startDate, DateTime endDate)
{
TimeSpan timeSpan = endDate - startDate;
int years = endDate.Year - startDate.Year;
int months = 12 - startDate.Month + (years - 1) * 12 + endDate.Month;
if (startDate.AddYears(years) > endDate)
years--;
startDate = startDate.AddYears(years);
if (startDate.Year == endDate.Year)
months = endDate.Month - startDate.Month;
else months = 12 - startDate.Month + endDate.Month;
if (startDate.AddMonths(months) > endDate)
months--;
string message = string.Empty;
int weeks = timeSpan.Days / 7;
int extraDay = timeSpan.Days % 7;
startDate = startDate.AddDays(weeks * 7 + extraDay);
timeSpan = endDate.Subtract(startDate);
int days = timeSpan.Days;
int hours = timeSpan.Hours;
int minutes = timeSpan.Minutes;
int seconds = timeSpan.Seconds;
if (years > 0 && months > 0 && days > 0)
{
message = years + " years " + months + " months " + days + " days ago";
}
else if (years > 0 && months > 0)
{
message = years + " years " + months + " months ago";
}
else if (months > 0 && days > 0)
{
message = months + " months " + days + " days ago";
}
else if (months > 0)
{
message = months + " months ago";
}
else if (weeks > 0)
{
message = weeks + " weeks ago";
}
else if (days > 0)
{
message = days + " days ago";
}
else if (hours > 0 && minutes > 0 && seconds > 0)
{
message = hours + " hours " + minutes + " minutes " + seconds + " seconds ago";
}
else if (hours > 0 && minutes > 0)
{
message = minutes + " minutes " + seconds + " seconds ago";
}
else if (minutes > 0 && seconds > 0)
{
message = minutes + " minutes " + seconds + " seconds ago";
}
else if (minutes > 0)
{
message = minutes + " minutes ago";
}
else
{
message = seconds + " seconds ago";
}
return message;
}
Screenshot