Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I need to calculate time difference between two time
The shift Start time is 04:30:Pm to 12:30AM
The Employee IN time is 08:30 AM then it shows the error message time expired

What I have tried:

VB
dtShifTime = Convert.ToDateTime("16:30").ToString("HH:mm")
   Dim dtEntryTime As DateTime = Convert.ToDateTime("08:30").ToString("HH:mm")
dtLateTime = dtShifTime.AddHours(2)
     If lblStartTime.Text <> "" And (dtShifTime  < dtEntryTime) Then
                    MBox("Time Expired")
End IF
Posted
Updated 16-Mar-19 2:27am
v6

 
Share this answer
 
Comments
[no name] 5-Mar-19 7:15am    
i tried in google but they did not match my requirements sir.
thats why i'm asking codeproject.
The problem is that your shift overlaps two days, with half an hour in the "second day".
so when you just convert a time to a DateTime object, you get the start time after the end time: 00:30 end, 16:30 start.

Then there is the string comparison ... why? Why convert strings to numbers and then back to strings in order to do a numeric comparison?

Instead, consider it as two shifts: 00:00 to 00:30 and 16:30 to 23:59 and compare for both.
The best way to do this is to consider the string starts as "minutes since midnight":
0 to 30, 990 to 1439 and convert the current time to the same thing: 510.
Then check if it is in either of the two shifts or not.

By the way: 08:30 is not in your shift at all ... so "time expired" would be correct...
 
Share this answer
 
Your problem is that you set 16:30 to variable dtShifTime, and then you use dtLateTime when comparing.
dtLateTime is not defined in this code.
Use the debugger to see the values of variables at runtime.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]

Debugging in Excel VBA - EASY Excel Macros[^]
MS Excel 2013: VBA Debugging Introduction[^]
How to debug Excel VBA - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
Quote:
I'm already using debugger for find How the code works but i've no idea for which condition to check Time difference because some condtitions applied

Since you work on hours only, you have a set of condition in the day, and another one when you change of day in the shift.
 
Share this answer
 
v2
Comments
[no name] 5-Mar-19 7:09am    
Sorry sir I forget to copy this line thats you said
dtLateTime = dtShifTime.AddHours(2)
Because every employee has 2 hours permission from his shift start time thats why i'm using this but i forget to paste it
Patrice T 5-Mar-19 7:24am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
[no name] 5-Mar-19 7:41am    
ok sir
[no name] 5-Mar-19 7:12am    
I'm already using debugger for find How the code works but i've no idea for which condition to check Time difference because some condtitions applied
Try this block of code ... This uses a timespan instance to check for lateness in minutes

Private Sub LatenessCheck()
      Dim dtShifTime As DateTime = Nothing
      Dim dtEntryTime As DateTime = Nothing
      Dim spLateness As TimeSpan

      If Date.TryParse("2019-01-01 16:30", dtShifTime) Then
        'dtShifTime is assigned
      End If
      If Date.TryParse("2019-01-01 08:30", dtEntryTime) Then
        'dtEntryTime is assigned
      End If
      If Not (dtShifTime = Nothing Or dtEntryTime = Nothing) Then
        spLateness = dtShifTime - dtEntryTime
        If spLateness.Minutes < 0 Then
          MsgBox("Late", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Late")
        End If
      Else
        'not late, or date assignment error
      End If
    End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900