Calculate difference in days with hours and minutes between two TextBoxes using C# and VB.Net in ASP.Net

Vanessa
 
on Feb 22, 2021 07:29 AM
Sample_101544.zip
1581 Views

Hi Guys,

Can you give a sample like two textbox like between txtbox1 and txtbox2 to calculate the number of days with hour and minutes?

Thank you.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Feb 22, 2021 11:56 PM
on Feb 22, 2021 11:57 PM

Hi Vanessa,

Check this example. Now please take its reference and correct your code.

HTML

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("[id*=txtFrom], [id*=txtTo]").datetimepicker({
            format: 'MM/DD/YYYY HH:mm'
        });
    });
</script>
<div class="container">
    <div class="row">
        <div class="col-md-3">
            <div class="form-group">
                From:
                <asp:TextBox ID="txtFrom" runat="server" />
            </div>
        </div>
        <div class="col-md-3">
            <div class="form-group">
                To:
                <asp:TextBox ID="txtTo" runat="server" />
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-3">
            <div class="form-group">
                <asp:Button Text="Difference" runat="server" OnClick="OnDifference" />
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-3">
            <div class="form-group">
                <asp:Label ID="lblResult" runat="server" />
            </div>
        </div>
    </div>
</div>

Code

C#

protected void OnDifference(object sender, EventArgs e)
{
    lblResult.Text = string.Empty;
    DateTime fromData = Convert.ToDateTime(txtFrom.Text.Trim());
    DateTime toData = Convert.ToDateTime(txtTo.Text.Trim());
    lblResult.Text = this.CalculateDifference(fromData, toData);
}

private string CalculateDifference(DateTime fromData, DateTime toData)
{
    TimeSpan ts = toData - fromData;
    string diff = "";
    if (ts.Days > 0)
    {
        diff = string.Format("{0} days, {1} hours, {2} minutes", ts.Days, ts.Hours, ts.Minutes);
    }
    else
    {
        diff = string.Format("{0} hours, {1} minutes", ts.Hours, ts.Minutes);
    }

    return diff;
}

VB.Net

Protected Sub OnDifference(ByVal sender As Object, ByVal e As EventArgs)
    lblResult.Text = String.Empty
    Dim fromData As DateTime = Convert.ToDateTime(txtFrom.Text.Trim())
    Dim toData As DateTime = Convert.ToDateTime(txtTo.Text.Trim())
    lblResult.Text = Me.CalculateDifference(fromData, toData)
End Sub

Private Function CalculateDifference(ByVal fromData As DateTime, ByVal toData As DateTime) As String
    Dim ts As TimeSpan = toData - fromData
    Dim diff As String = ""
    If ts.Days > 0 Then
        diff = String.Format("{0} days, {1} hours, {2} minutes", ts.Days, ts.Hours, ts.Minutes)
    Else
        diff = String.Format("{0} hours, {1} minutes", ts.Hours, ts.Minutes)
    End If

    Return diff
End Function

Screenshot