Hi Vanessa,
Refer below sample.
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;
string fromData = txtFrom.Text.Trim();
string toData = txtTo.Text.Trim();
lblResult.Text = this.timecal(fromData, toData);
}
private string timecal(string startDate, string endDate)
{
DateTime fromDate = DateTime.Parse(startDate);
DateTime toDate = DateTime.Parse(endDate);
if (fromDate > toDate)
{
toDate = toDate.AddDays(1);
}
string diff;
TimeSpan ts = toDate - fromDate;
if (ts.Days > 0)
{
diff = string.Format("{0} hours: {1} minutes", ts.Days * 24 + 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 String = txtFrom.Text.Trim()
Dim toData As String = txtTo.Text.Trim()
lblResult.Text = Me.timecal(fromData, toData)
End Sub
Private Function timecal(ByVal startDate As String, ByVal endDate As String) As String
Dim fromDate As DateTime = DateTime.Parse(startDate)
Dim toDate As DateTime = DateTime.Parse(endDate)
If fromDate > toDate Then
toDate = toDate.AddDays(1)
End If
Dim diff As String
Dim ts As TimeSpan = toDate - fromDate
If ts.Days > 0 Then
diff = String.Format("{0} hours: {1} minutes", ts.Days * 24 + ts.Hours, ts.Minutes)
Else
diff = String.Format("{0} hours: {1} minutes", ts.Hours, ts.Minutes)
End If
Return diff
End Function
Screenshot