---
title: "Calculate Duration between two dates using Asp.Net C#"  
description: "Hi everyone in this article, I’m explaining about calculate duration betweent two dates."  
author: "Anonymous User"  
published: 2014-11-25  
updated: 2020-04-10  
canonical: https://www.mindstick.com/articles/1539/calculate-duration-between-two-dates-using-asp-dot-net-c-sharp  
category: "asp.net"  
tags: ["c#", "date"]  
reading_time: 3 minutes  

---

# Calculate Duration between two dates using Asp.Net C#

## Calculate Duration between two dates using Asp.Net C#

Hi everyone in this article, I’m explaining about calculate the [duration between two](https://www.mindstick.com/Articles/1091/ienumerable-ienumerator-icollection-and-ilist-in-c-sharp) dates.\

##### Description:

I was looking for a Duration [calculator](https://www.mindstick.com/articles/38/calculator-in-asp-dot-net-using-javascript) 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](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available) >> New >> File >> Website

![Calculate Duration between two dates using Asp.Net C#](https://www.mindstick.com/mindstickarticle/78643370-e363-4030-af8d-344020d66837/images/d1b71c15-47b8-495e-8d2f-00ca6a78a30c.png)

Give [Application](https://www.mindstick.com/articles/12824/calculator-application-in-android) Name “DurationCalculate”

![Calculate Duration between two dates using Asp.Net C#](https://www.mindstick.com/mindstickarticle/78643370-e363-4030-af8d-344020d66837/images/6df8eaf1-b8e4-402d-a3fc-1acc6b001661.png)

Step 2

Add a WebForms in your project

![Calculate Duration between two dates using Asp.Net C#](https://www.mindstick.com/mindstickarticle/78643370-e363-4030-af8d-344020d66837/images/6835151e-c08f-46c1-bb24-46ba4a5c8510.png)

Your [aspx page](https://www.mindstick.com/forum/218/how-do-i-create-an-aspx-page-that-periodically-refreshes-itself)

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

Preview

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

**![Calculate Duration between two dates using Asp.Net C#](https://www.mindstick.com/mindstickarticle/78643370-e363-4030-af8d-344020d66837/images/dd834d3e-9ae0-4542-b2b0-247ff491cbff.png)**

Now open the default.aspx.cs page and add the following code in the calculate [button click](https://www.mindstick.com/forum/568/check-condion-before-post-page-on-button-click-in-jquery-or-javascript) event.

Read two input TextBox texts and assign them to a [DateTime data](https://www.mindstick.com/forum/266/datetime-data-type) 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 [calendar](https://www.mindstick.com/articles/13088/how-to-choose-the-best-calendar-for-you-dynamics-crm-system) as in the following:

#### Output

**![Calculate Duration between two dates using Asp.Net C#](https://www.mindstick.com/mindstickarticle/78643370-e363-4030-af8d-344020d66837/images/8362401c-182d-4682-b022-355a4249fd03.png)**

Now from all the preceding 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\

---

Original Source: https://www.mindstick.com/articles/1539/calculate-duration-between-two-dates-using-asp-dot-net-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
