Calculate working Hours between two Times in ASP.Net using C# and VB.Net

Vanessa
 
on Jul 13, 2021 10:56 PM
Sample_798712.zip
1473 Views

Helllo,

I have a sample code below and it was work properly, my problem is how reduce the calculation into 8 working hours only instead of 24hours per day?

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

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jul 14, 2021 04:58 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;
    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.Hours > 0)
    {
        if (ts.Hours / 8 > 0)
        {
            diff = string.Format("{0} days {1} hours {2} minutes",
                Math.Round(Convert.ToDecimal(ts.Hours / 8)),
                ts.Hours - (Math.Round(Convert.ToDecimal(ts.Hours / 8)) * 8),
                ts.Minutes);
        }
        else if (ts.Hours / 8 == 0)
        {
            diff = string.Format("{0} hours {1} minutes", ts.Hours, ts.Minutes);
        }
    }
    else if (ts.Minutes > 0)
    {
        diff = string.Format("{0} minutes", 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.Hours > 0 Then
        If ts.Hours / 8 > 0 Then
            diff = String.Format("{0} days {1} hours {2} minutes",
                                    Math.Round(Convert.ToDecimal(ts.Hours / 8)),
                                    ts.Hours - (Math.Round(Convert.ToDecimal(ts.Hours / 8)) * 8),
                                    ts.Minutes)
        ElseIf ts.Hours / 8 = 0 Then
            diff = String.Format("{0} hours {1} minutes", ts.Hours, ts.Minutes)
        End If
    ElseIf ts.Minutes > 0 Then
        diff = String.Format("{0} minutes", ts.Minutes)
    End If

    Return diff
End Function