title: Subtract time from Business hours only (VB.net)
body: <p>I am finding this concept hard to figure out. The scenario should go like this:</p>
  
  <p>The picture is a datagridview contains 3 columns (Today, Tomorrow and The next day)</p>
  
  <p>Let's say I've subtract the hours but if I will plot on 8am on column No. 1 (Tomorrow) then the difference will start on 17:00 (5pm) Column No. 0 (Today.</p>
  
  <p>I only need the snippet code since I know the source code for subtraction already. </p>
  
  <p>Datagridview :</p>
  
  <p><img src="https://i.stack.imgur.com/7TG9c.png"/></p>
  
  <p>EDIT: To avoid confusion, how to plot 14:00  on Today column 0 of Datagridview if I start to subtract from 08:00 of Column 1. (Only Business Hours from 8am to 5pm)</p>
  
  <p>EDIT2: Source code as requested:</p>
  
  <pre><code>    For i As Integer = 0 To frmSchedule.DatagridSchedule.Rows.Count - 1
              If cmbSelectAvailableTIme.SelectedItem.Equals(frmSchedule.DatagridSchedule.Rows(i).HeaderCell.Value) Then
                  frmSchedule.DatagridSchedule.Rows(i).Cells(0).Value = Info1 &amp; Environment.NewLine &amp; Info2 &amp; Environment.NewLine &amp; Info3 &amp; Environment.NewLine &amp; txtboxAccessories.Text.ToString &amp; Environment.NewLine &amp; INfo4
                  frmSchedule.DatagridSchedule.DefaultCellStyle.WrapMode = DataGridViewTriState.True
  
                  Dim datetime1 As DateTime = cmbSelectAvailableTIme.Text
                  Dim newDateTime As DateTime = datetime1.Subtract(New TimeSpan(0, TimeEstimate.Text, 0)).ToString("HH:mm")
  
                  StatusBillboard.DataGridViewBillboard.Rows(i).Cells(0).Value = Info1
                  StatusBillboard.DataGridViewBillboard.Rows(i).Cells(1).Value = Info2
                  StatusBillboard.DataGridViewBillboard.Rows(i).Cells(2).Value = Info2
                  StatusBillboard.DataGridViewBillboard.Rows(i).Cells(3).Value = INfo4
                  StatusBillboard.DataGridViewBillboard.Rows(i).Cells(4).Value = "Invoice"
                  StatusBillboard.DataGridViewBillboard.Rows(i).Cells(5).Value = "MP"
                  StatusBillboard.DataGridViewBillboard.Rows(i).Cells(6).Value = DateTime.Now.AddDays(1).ToString("MM/dd/yyyy") &amp; " " &amp; newDateTime
  
  
  
                  For i_scan As Integer = 0 To FrmFinalApproval.DatagridFinalApproval.Rows.Count - 1
                      For i_inspection As Integer = 0 To PreInspection.DatagridInspection.Rows.Count - 1
                          If cmbSelectAvailableTIme.SelectedItem.Equals(FrmFinalApproval.DatagridFinalApproval.Rows(i_scan).HeaderCell.Value) Then
                              cmbSelectAvailableTIme.Text = ""
                              cmbSelectAvailableTIme.Text = Format(newDateTime, "HH:mm")
                              Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(cmbSelectAvailableTIme.Text)
                              FrmFinalApproval.DatagridFinalApproval.Rows(selectedIndex).Cells(0).Value = Info1 &amp; Environment.NewLine &amp; Info2 &amp; Environment.NewLine &amp; Info3 &amp; Environment.NewLine &amp; txtboxAccessories.Text.ToString &amp; Environment.NewLine &amp; INfo4
                              FrmFinalApproval.DatagridFinalApproval.DefaultCellStyle.WrapMode = DataGridViewTriState.True
                              '   cell.Style.BackColor = Color.LightSalmon
                              FrmFinalApproval.DatagridFinalApproval.Rows(selectedIndex).Cells(0).Style.BackColor = Color.LightYellow
                              FinalGridview = FrmFinalApproval.DatagridFinalApproval.Rows(selectedIndex).Cells(0).RowIndex
                              FInalGridview2 = FrmFinalApproval.DatagridFinalApproval.Rows(selectedIndex).Cells(0).ColumnIndex
  
                          End If
  
  
  
                          If cmbSelectAvailableTIme.SelectedItem.Equals(PreInspection.DatagridInspection.Rows(i_inspection).HeaderCell.Value) Then
                              cmbSelectAvailableTIme.Text = ""
  
                              cmbSelectAvailableTIme.Text = Format(newDateTime, "HH:mm")
  
                              Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(cmbSelectAvailableTIme.Text)
  
                              PreInspection.DatagridInspection.Rows(selectedIndex).Cells(0).Value = Info1 &amp; Environment.NewLine &amp; Info2 &amp; Environment.NewLine &amp; Info3 &amp; Environment.NewLine &amp; txtboxAccessories.Text.ToString &amp; Environment.NewLine &amp; INfo4
                              PreInspection.DatagridInspection.DefaultCellStyle.WrapMode = DataGridViewTriState.True
  
                              PreInspection.DatagridInspection.Rows(selectedIndex).Cells(0).Style.BackColor = Color.LightYellow
                              carwashInspect = PreInspection.DatagridInspection.Rows(selectedIndex).Cells(0).RowIndex
                              Carwashinspect2 = PreInspection.DatagridInspection.Rows(selectedIndex).Cells(0).ColumnIndex
                          End If
  
                      Next
                  Next
  
                  Dim datetime2 As DateTime = cmbSelectAvailableTIme.Text
                  Dim newDateTime2 As DateTime = datetime2.Subtract(New TimeSpan(0, TimeEstimate.Text, 0)).ToString("HH:mm")
                  cmbSelectAvailableTIme.Text = newDateTime2.ToString("HH:mm")
  
  
  
          For i_scan2 As Integer = 0 To FrmCandCollection.DatagridCandCollection.Rows.Count - 1
                      For i_carwash As Integer = 0 To Carwash.DatagridCarwash.Rows.Count - 1
                          If cmbSelectAvailableTIme.SelectedItem.Equals(Carwash.DatagridCarwash.Rows(i_scan2).HeaderCell.Value) Then
                              Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(Carwash.DatagridCarwash.Rows(i_scan2).HeaderCell.Value)
                              FrmCandCollection.DatagridCandCollection.Rows(selectedIndex).Cells(0).Value = Info1 &amp; Environment.NewLine &amp; Info2 &amp; Environment.NewLine &amp; Info3 &amp; Environment.NewLine &amp; txtboxAccessories.Text.ToString &amp; Environment.NewLine &amp; INfo4
                              FrmCandCollection.DatagridCandCollection.DefaultCellStyle.WrapMode = DataGridViewTriState.True
                              CandCGridview = FrmCandCollection.DatagridCandCollection.Rows(selectedIndex).Cells(0).RowIndex
                              CandCGridview2 = FrmCandCollection.DatagridCandCollection.Rows(selectedIndex).Cells(0).ColumnIndex
                          End If
  
                          If cmbSelectAvailableTIme.SelectedItem.Equals(Carwash.DatagridCarwash.Rows(i_carwash).HeaderCell.Value) Then
  
  
                              Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(Carwash.DatagridCarwash.Rows(i_carwash).HeaderCell.Value)
  
                              Carwash.DatagridCarwash.Rows(selectedIndex).Cells(0).Value = Info1 &amp; Environment.NewLine &amp; Info2 &amp; Environment.NewLine &amp; Info3 &amp; Environment.NewLine &amp; txtboxAccessories.Text.ToString &amp; Environment.NewLine &amp; INfo4
                              Carwash.DatagridCarwash.DefaultCellStyle.WrapMode = DataGridViewTriState.True
  
                              CarwashGridview = Carwash.DatagridCarwash.Rows(selectedIndex).Cells(0).RowIndex
                              CarwashGridview2 = Carwash.DatagridCarwash.Rows(selectedIndex).Cells(0).ColumnIndex
  
  
                          End If
  
                      Next
  
                  Next
  
                  Dim datetime3 As DateTime = cmbSelectAvailableTIme.Text
                  Dim newDateTime3 As DateTime = datetime3.Subtract(New TimeSpan(0, TimeEstimate.Text, 0)).ToString("HH:mm")
                  cmbSelectAvailableTIme.Text = newDateTime3.ToString("HH:mm")
  
  
  
  
  
    For i_scan3 As Integer = 0 To frmAccounting.DatagridAccounting.Rows.Count - 1
                      For i_caraccessories As Integer = 0 To frmAccessories.DatagridviewAccessories.Rows.Count - 1
                          For i_cashier As Integer = 0 To frmCashier.DatagridCashier.Rows.Count - 1
  
                              If cmbSelectAvailableTIme.SelectedItem.Equals(frmAccounting.DatagridAccounting.Rows(i_scan3).HeaderCell.Value) Then
                                  Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(frmAccounting.DatagridAccounting.Rows(i_scan3).HeaderCell.Value)
                                  frmAccounting.DatagridAccounting.Rows(selectedIndex).Cells(0).Value = Info1 &amp; Environment.NewLine &amp; Info2 &amp; Environment.NewLine &amp; Info3 &amp; Environment.NewLine &amp; txtboxAccessories.Text.ToString &amp; Environment.NewLine &amp; INfo4
                                  frmAccounting.DatagridAccounting.DefaultCellStyle.WrapMode = DataGridViewTriState.True
  
                                  AccountGridview = frmAccounting.DatagridAccounting.Rows(selectedIndex).Cells(0).RowIndex
                                  AccountGridView2 = frmAccounting.DatagridAccounting.Rows(selectedIndex).Cells(0).ColumnIndex
                              End If
  
                              If cmbSelectAvailableTIme.SelectedItem.Equals(frmAccessories.DatagridviewAccessories.Rows(i_caraccessories).HeaderCell.Value) Then
  
  
                                  Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(frmAccessories.DatagridviewAccessories.Rows(i_caraccessories).HeaderCell.Value)
  
                                  frmAccessories.DatagridviewAccessories.Rows(selectedIndex).Cells(0).Value = Info1 &amp; Environment.NewLine &amp; Info2 &amp; Environment.NewLine &amp; Info3 &amp; Environment.NewLine &amp; txtboxAccessories.Text.ToString &amp; Environment.NewLine &amp; INfo4
                                  frmAccessories.DatagridviewAccessories.DefaultCellStyle.WrapMode = DataGridViewTriState.True
                                  AccessoriesGridview = frmAccessories.DatagridviewAccessories.Rows(selectedIndex).Cells(0).RowIndex
                                  AccessoriesGridview2 = frmAccessories.DatagridviewAccessories.Rows(selectedIndex).Cells(0).ColumnIndex
                              End If
  
                              If cmbSelectAvailableTIme.SelectedItem.Equals(frmCashier.DatagridCashier.Rows(i_cashier).HeaderCell.Value) Then
                                  Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(frmCashier.DatagridCashier.Rows(i_cashier).HeaderCell.Value)
  
                                  frmCashier.DatagridCashier.Rows(selectedIndex).Cells(0).Value = Info1 &amp; Environment.NewLine &amp; Info2 &amp; Environment.NewLine &amp; Info3 &amp; Environment.NewLine &amp; txtboxAccessories.Text.ToString &amp; Environment.NewLine &amp; INfo4
                                  frmCashier.DatagridCashier.DefaultCellStyle.WrapMode = DataGridViewTriState.True
                                  AccessoriesGridview = frmCashier.DatagridCashier.Rows(selectedIndex).Cells(0).RowIndex
                                  AccessoriesGridview2 = frmCashier.DatagridCashier.Rows(selectedIndex).Cells(0).ColumnIndex
                              End If
                          Next
                      Next
                  Next
  
  
                  Dim datetime4 As DateTime = cmbSelectAvailableTIme.Text
                  Dim newDateTime4 As DateTime = datetime4.Subtract(New TimeSpan(0, TimeEstimate.Text, 0)).ToString("HH:mm")
                  cmbSelectAvailableTIme.Text = newDateTime4.ToString("HH:mm")
  
   For i_scan4 As Integer = 0 To FrmInsurance.DatagridInsurance.Rows.Count - 1
                      If cmbSelectAvailableTIme.SelectedItem.Equals(FrmInsurance.DatagridInsurance.Rows(i_scan4).HeaderCell.Value) Then
  
                          Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(cmbSelectAvailableTIme.Text)
  
                          FrmInsurance.DatagridInsurance.Rows(selectedIndex).Cells(0).Value = Info1 &amp; Environment.NewLine &amp; Info2 &amp; Environment.NewLine &amp; Info3 &amp; Environment.NewLine &amp; txtboxAccessories.Text.ToString &amp; Environment.NewLine &amp; INfo4
                          FrmInsurance.DatagridInsurance.DefaultCellStyle.WrapMode = DataGridViewTriState.True
                          InsuranceGridView = FrmInsurance.DatagridInsurance.Rows(selectedIndex).Cells(0).RowIndex
                          InsuranceGridview2 = FrmInsurance.DatagridInsurance.Rows(selectedIndex).Cells(0).ColumnIndex
                      End If
  
                  Next
  
  
                  Dim datetime5 As DateTime = cmbSelectAvailableTIme.Text
                  Dim newDateTime5 As DateTime = datetime5.Subtract(New TimeSpan(0, TimeEstimate.Text, 0)).ToString("HH:mm")
                  cmbSelectAvailableTIme.Text = newDateTime5.ToString("HH:mm")
  
                  For i_scan5 As Integer = 0 To frmFinancing.DatagridFinancing.Rows.Count - 1
                      If cmbSelectAvailableTIme.SelectedItem.Equals(frmFinancing.DatagridFinancing.Rows(i_scan5).HeaderCell.Value) Then
  
                          Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(cmbSelectAvailableTIme.Text)
  
                          frmFinancing.DatagridFinancing.Rows(selectedIndex).Cells(0).Value = Info1 &amp; Environment.NewLine &amp; Info2 &amp; Environment.NewLine &amp; Info3 &amp; Environment.NewLine &amp; txtboxAccessories.Text.ToString &amp; Environment.NewLine &amp; INfo4
                          frmFinancing.DatagridFinancing.DefaultCellStyle.WrapMode = DataGridViewTriState.True
                          FinancingGridView = frmFinancing.DatagridFinancing.Rows(selectedIndex).Cells(0).RowIndex
                          FinancingGRidview2 = frmFinancing.DatagridFinancing.Rows(selectedIndex).Cells(0).ColumnIndex
                      End If
  
                  Next
  
                  Dim datetime6 As DateTime = cmbSelectAvailableTIme.Text
                  Dim newDateTime6 As DateTime = datetime6.Subtract(New TimeSpan(0, TimeEstimate.Text, 0)).ToString("HH:mm")
                  cmbSelectAvailableTIme.Text = newDateTime6.ToString("HH:mm")
  
                  For i_scan6 As Integer = 0 To frmINvoice.DatagridInvoice.Rows.Count - 1
                      If cmbSelectAvailableTIme.SelectedItem.Equals(frmINvoice.DatagridInvoice.Rows(i_scan6).HeaderCell.Value) Then
  
                          Dim selectedIndex As Integer = cmbSelectAvailableTIme.FindStringExact(cmbSelectAvailableTIme.Text)
  
                          frmINvoice.DatagridInvoice.Rows(selectedIndex).Cells(0).Value = Info1 &amp; Environment.NewLine &amp; Info2 &amp; Environment.NewLine &amp; Info3 &amp; Environment.NewLine &amp; txtboxAccessories.Text.ToString &amp; Environment.NewLine &amp; INfo4
                          frmINvoice.DatagridInvoice.DefaultCellStyle.WrapMode = DataGridViewTriState.True
                          InvoiceGridView = frmINvoice.DatagridInvoice.Rows(selectedIndex).Cells(0).RowIndex
                          InvoiceGridView2 = frmINvoice.DatagridInvoice.Rows(selectedIndex).Cells(0).ColumnIndex
                      End If
  
                  Next
  
  
  
  
  
  
  
              End If
  
  
          Next
  </code></pre>