Results 1 to 5 of 5

Thread: [resolved]Issue with determining time

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Location
    Seminole, FL
    Posts
    30

    [resolved]Issue with determining time

    In my app I've written I'm trying to determine what shift it is. The issue I'm having is that after midnight my code doesn't determine what shift it is. I think it's because my constants are assigning date and time when I hover over them even though I'm only assigning time. I'm still pretty new to .Net so I'm kind of learning on the fly. Here is my code.

    Code:
        Dim DateAndTime As String
        Dim Time1 As DateTime = DateTime.Parse("06:00:00")
        Dim Time2 As DateTime = DateTime.Parse("14:00:00")
        Dim Time3 As DateTime = DateTime.Parse("22:00:00")
        Dim CurrentShift As String
        Dim i As Short
    
            DateAndTime = Now
    
            If Is1stShift(Time1, Time2) = True Then
                CurrentShift = "1"
            End If
    
            If Is2ndShift(Time2, Time3) = True Then
                CurrentShift = "2"
            End If
    
            If Is3rdShift(Time3, Time1) = True Then
                CurrentShift = "3"
            End If
    
        Public Function Is1stShift(ByVal Time1 As DateTime, ByVal Time2 As DateTime) As Boolean
            Return DateTime.Now.TimeOfDay >= Time1.TimeOfDay AndAlso DateTime.Now.TimeOfDay <= Time2.TimeOfDay
        End Function
    
        Public Function Is2ndShift(ByVal Time2 As DateTime, ByVal Time3 As DateTime) As Boolean
            Return DateTime.Now.TimeOfDay >= Time2.TimeOfDay AndAlso DateTime.Now.TimeOfDay <= Time3.TimeOfDay
        End Function
    
        Public Function Is3rdShift(ByVal Time3 As DateTime, ByVal Time1 As DateTime) As Boolean
            Return DateTime.Now.TimeOfDay >= Time3.TimeOfDay AndAlso DateTime.Now.TimeOfDay <= Time1.TimeOfDay
        End Function
    Last edited by N20D5OH; Dec 17th, 2008 at 07:11 AM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: Issue with determining time

    try this:

    vb Code:
    1. Public Function CurrentShift() As String
    2.     Dim timeNow As New TimeSpan(Now.Hour, Now.Minute, Now.Second)
    3.     Dim Time1 As New TimeSpan(6, 0, 0)
    4.     Dim Time2 As New TimeSpan(14, 0, 0)
    5.     Dim Time3 As New TimeSpan(22, 0, 0)
    6.  
    7.     If timeNow >= Time1 And timeNow < Time2 Then
    8.         Return "1"
    9.     ElseIf timeNow >= Time2 And timeNow < Time3 Then
    10.         Return "2"
    11.     ElseIf timeNow >= Time3 And timeNow <= New TimeSpan(24, 0, 0) Or timeNow >= New TimeSpan(0, 0, 0) And timeNow < Time1 Then
    12.         Return "3"
    13.     End If
    14.  
    15.     Return String.Empty
    16.  
    17. End Function

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Location
    Seminole, FL
    Posts
    30

    [Resolved]Issue with determining time

    Quote Originally Posted by .paul.
    try this:

    vb Code:
    1. Public Function CurrentShift() As String
    2.     Dim timeNow As New TimeSpan(Now.Hour, Now.Minute, Now.Second)
    3.     Dim Time1 As New TimeSpan(6, 0, 0)
    4.     Dim Time2 As New TimeSpan(14, 0, 0)
    5.     Dim Time3 As New TimeSpan(22, 0, 0)
    6.  
    7.     If timeNow >= Time1 And timeNow < Time2 Then
    8.         Return "1"
    9.     ElseIf timeNow >= Time2 And timeNow < Time3 Then
    10.         Return "2"
    11.     ElseIf timeNow >= Time3 And timeNow <= New TimeSpan(24, 0, 0) Or timeNow >= New TimeSpan(0, 0, 0) And timeNow < Time1 Then
    12.         Return "3"
    13.     End If
    14.  
    15.     Return String.Empty
    16.  
    17. End Function

    Thank you. I actually had just figured out a work around, but your way is much less code. I have a lot to learn about .Net, there are so many built in features that weren't available in VB6.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [resolved]Issue with determining time

    I haven't looked at your conditions too carefully but I think this is pretty close to what you want:
    vb.net Code:
    1. Public Enum Shift
    2.     TenToSix
    3.     SixToTwo
    4.     TwoToTen
    5. End Enum
    vb.net Code:
    1. Dim currentShift As Shift
    2.  
    3. Select Case Date.Now.Hour
    4.     Case Is < 6, Is >= 22
    5.         currentShift = Shift.TenToSix
    6.     Case Is < 14
    7.         currentShift = Shift.SixToTwo
    8.     Case Else
    9.         currentShift = Shift.TwoToTen
    10. End Select
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Location
    Seminole, FL
    Posts
    30

    Re: [resolved]Issue with determining time

    Thanks again. It's amazing how much easier stuff is in .Net. I'm taking a 5 day crash course right after the first of the year. I don't develop full time, just a hack.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width