I am working on an application in VB.NET for a manufacturing unit that runs in shifts. At the end of every shift, the application should give an alarm indicating that the shift has ended.
In the application, the user keys-in shift time in Hours & Minutes textboxes and saves in a database on Save button click.

As shown in the picture, the First shift ends at 03:30 p.m., Second at 11:30 p.m. & the Third shift at 07:30 a.m. When the application loads, Hours & Minutes of every shift are stored in the variables.
My query is, how do I convert the values from these variables into time format (HH:mm) so that I can compare the shift time with system time and raise an alarm when the shift ends everyday irrespective of the date. Below is a code that I have tried so far..
Private Sub TmrMonitor_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TmrMonitor.Tick
Dim Tim As String = TxtHH.Text & ":" & TxtMM.Text
LblShift.Text = (DateTime.ParseExact(Tim, "hh:mm", CultureInfo.InvariantCulture)) = DateTime.Now()
TmrMonitor.Stop()
ReadReg() ' Read data from PLC
If FormActive = True Then
TmrMonitor.Start()
End If
CommStat() ' PLC communication status
End Sub
Thanks in advance.
Prashant.
Change
LblShift.Text = (DateTime.ParseExact(Tim, "hh:mm", CultureInfo.InvariantCulture)) = DateTime.Now()
To
Dim Tim As String = TxtHH.Text & ":" & TxtMM.Text
LblShift.Text = (DateTime.ParseExact(Tim, "HH:mm", CultureInfo.InvariantCulture))
Dim shiftTime = TimeSpan.Parse(Tim)
Dim systemtime = TimeSpan.Parse(Now.ToString("HH:mm"))
If (shiftTime.Equals(systemtime) = False) Then
MessageBox.Show("times don't match")
End If
the issue is that when using DateTime.ParseExact it returns date in format MM/dd/yyyy hh:mm:ss tt. But in your case you want just the HH:mm format so use the following piece of code to compare and it will work. Please note this is just a simple approach as you have already specified you do not care about date.
LblShift.Text = Tim = Format(DateTime.Now, "HH:mm")
hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With