3

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.

enter image description here

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.

Prashant
  • 93
  • 9
  • 1
    Just to be clear. do you want to append the hours and minutes together, so they form HH:mm format. Thus value in textbox `15` and in `30` shows up as `15:30`? – boop_the_snoot Aug 30 '17 at 04:24
  • Yes, but when I append these values, I am not being able to compare system date & shift times. – Prashant Aug 30 '17 at 04:29
  • Okay, post the codes you have tried for **when I append these values, I am not being able to compare system date & shift times**. Edit the question, and then post the codes in it – boop_the_snoot Aug 30 '17 at 04:30
  • Kindly [edit your question](https://stackoverflow.com/posts/45951861/edit) and post the codes you have tried! – boop_the_snoot Aug 30 '17 at 04:37
  • add comment on your code please. I don't undertand what you're doing there with `ReadReg` and `FormActive` and others too. – Subaz Aug 30 '17 at 04:46

2 Answers2

3

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
boop_the_snoot
  • 3,209
  • 4
  • 33
  • 44
1

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.

akhil kumar
  • 1,598
  • 1
  • 13
  • 26