# [Solved] Calculate difference between two Times returns negative value in ASP.Net

on Mar 10, 2021 12:27 AM
Sample_121593.zip
1138 Views

Hello,

I have a problem when my txtDateofApplication is pm or afternoon and txtDateReceivedClient is am or morning.

here is the code

```Dim d1 As DateTime = Convert.ToDateTime(txtDateofApplication.Text)
Dim d2 As DateTime = Convert.ToDateTime(txtDateReceivedClient.Text)
Dim totalDays = (d2 - d1).Days
Dim wd As Integer = 0
For x As Integer = 0 To totalDays
Dim currendDate As DateTime = d1.AddDays(x)
If currendDate.DayOfWeek <> DayOfWeek.Saturday Then
wd += 1
End If
ElseIf currendDate.DayOfWeek <> DayOfWeek.Sunday Then
wd += 1
End If
End If
Next

Public Property result As String
Public Sub timecal(ByVal startdate As String, ByVal enddate As String)
Dim ts As TimeSpan = DateTime.Parse(enddate) - DateTime.Parse(startdate)
Dim lbl As String = ""
If ts.Days > 0 Then
lbl = String.Format("{0} hours: {1} minutes", ts.Days * 24 + ts.Hours, ts.Minutes)
Else
lbl = String.Format("{0} hours: {1} minutes", ts.Hours, ts.Minutes)
End If

result = lbl
txtHoursMinTAT.Text = lbl
End Sub

Protected Sub txtHoursMinReceivedClient_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtHoursMinReceivedClient.TextChanged
'Response.Write(result)
End Sub```

on Mar 10, 2021 01:48 AM

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://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<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)
{
}
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