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

Vanessa
 
on Feb 28, 2021 10:48 PM
Sample_693540.zip
2301 Views

@dharmendr

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

This is very awesome sample, but can give a code that only hours and minutes are choices instead of date then it will calculated the hours and minutes?

Thanks you.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Mar 01, 2021 03:24 AM
on Mar 01, 2021 03:25 AM

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: '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" CssClass="form-control" />
            </div>
        </div>
        <div class="col-md-3">
            <div class="form-group">
                To:
        <asp:TextBox ID="txtTo" runat="server" CssClass="form-control" />
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-3">
            <div class="form-group">
                <asp:Button Text="Difference" runat="server" OnClick="OnDifference" CssClass="btn btn-primary" />
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-3">
            <div class="form-group">
                <asp:Label ID="lblResult" runat="server" CssClass="label label-success"  />
            </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 = 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 = String.Format("{0} hours, {1} minutes", ts.Hours, ts.Minutes)

    Return diff
End Function

Screenshot