Hi everyone in this article, I’m explaining about calculate duration betweent two dates.


I was looking for a Duration calculator between two dates which gives duration in number of years, number of months and number of days together. It can be defined as Age Calculator which is able to give any one’s exact age in years, months and days.

But unfortunately I didn't find exactly what I was looking for. Most of the calculators give output either in years or in months or in weeks or in days and so on and even in milliseconds, but not all the information together.

I realize that there should be something to calculate duration between two dates and give output in years, months and days together.

Step 1

Open visual studio >> New >> File >> Website

Give Application Name “DurationCalculate”


Step 2

Add a WebForms in your project

Your aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<body style="background-color: #ed9a33">
    <form id="form1" runat="server">
        <table style="padding: 30px; color: White; margin-top: 30px">
                <td>From Date 
                    <asp:TextBox ID="FromYearTxt" runat="server" OnTextChanged="FromYearTxt_TextChanged"></asp:TextBox>
                <td>To Date 
                    <asp:TextBox ID="ToYearTxt" runat="server"></asp:TextBox>
        <div style="margin-left: 180px">
            <asp:Button ID="btncalculate" runat="server" Text="Calculate" OnClick="btncalculate_Click" />
        <table style="background-color: #9d84cc; color: White; padding: 30px; margin-top: 20px; margin-left: 15px"
            id="tblResults" runat="server">
                <td id="tdYear" runat="server"></td>
                <td>Total Months 
                <td id="tdMonths" runat="server"></td>
                <td>Total Days 
                <td id="tdDays" runat="server"></td>
                <td>Total Hours 
                <td id="tdHrs" runat="server"></td>
                <td>Total Minutes                  </td>
                <td id="tdminuts" runat="server"></td>
                <td>Total Seconds                  </td>
                <td id="tdseconds" runat="server"></td>
                <td>Total MileSesonds                  </td>
                <td id="tdmileSec" runat="server"></td>





Now switch to design mode; it will look such as follows.




Now open the default.aspx.cs page and add the following code in the calculate

button click event.

Read two input TextBox texts and assign them to a DateTime data type variable as

in the following:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        tblResults.Visible = false;
    protected void btncalculate_Click(object sender, EventArgs e)
        if (FromYearTxt.Text != "" && ToYearTxt.Text != "")
            DateTime FromYear = Convert.ToDateTime(FromYearTxt.Text);
            DateTime ToYear = Convert.ToDateTime(ToYearTxt.Text);
            TimeSpan objTimeSpan = ToYear - FromYear;
            int Years = ToYear.Year - FromYear.Year;
            int month = ToYear.Month - FromYear.Month;
            double Days = Convert.ToDouble(objTimeSpan.TotalDays);
            int TotalMonths = (Years * 12) + month;
            double TotalHours = objTimeSpan.TotalHours;
            double TotalMinutes = objTimeSpan.TotalMinutes;
            double TotalSeconds = objTimeSpan.TotalSeconds;
            double TotalMileSeconds = objTimeSpan.TotalMilliseconds;
            tdYear.InnerText = Years + "  Year  " + month + "  Months";
            tdMonths.InnerText = Convert.ToString(TotalMonths);
            tdDays.InnerText = Convert.ToString(Days);
            tdHrs.InnerText = Convert.ToString(TotalHours);
            tdminuts.InnerText = Convert.ToString(TotalMinutes);
            tdseconds.InnerText = Convert.ToString(TotalSeconds);
            tdmileSec.InnerText = Convert.ToString(TotalMileSeconds);
            tblResults.Visible = true;



Now enter a From date and To date using the calender as in the following:






Now from all the receding examples we have learned how to calculate the years,

months, days, minutes, seconds & milliseconds between two dates

In my next post I’ll explain about Implement Captcha in ASP.NET MVC4

  Modified On Nov-30-2017 03:48:08 AM

Leave Comment