|
-
Dec 17th, 2008, 06:01 AM
#1
Thread Starter
Junior Member
[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.
-
Dec 17th, 2008, 06:49 AM
#2
Re: Issue with determining time
try this:
vb Code:
Public Function CurrentShift() As String
Dim timeNow As New TimeSpan(Now.Hour, Now.Minute, Now.Second)
Dim Time1 As New TimeSpan(6, 0, 0)
Dim Time2 As New TimeSpan(14, 0, 0)
Dim Time3 As New TimeSpan(22, 0, 0)
If timeNow >= Time1 And timeNow < Time2 Then
Return "1"
ElseIf timeNow >= Time2 And timeNow < Time3 Then
Return "2"
ElseIf timeNow >= Time3 And timeNow <= New TimeSpan(24, 0, 0) Or timeNow >= New TimeSpan(0, 0, 0) And timeNow < Time1 Then
Return "3"
End If
Return String.Empty
End Function
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 17th, 2008, 06:58 AM
#3
Thread Starter
Junior Member
[Resolved]Issue with determining time
 Originally Posted by .paul.
try this:
vb Code:
Public Function CurrentShift() As String
Dim timeNow As New TimeSpan(Now.Hour, Now.Minute, Now.Second)
Dim Time1 As New TimeSpan(6, 0, 0)
Dim Time2 As New TimeSpan(14, 0, 0)
Dim Time3 As New TimeSpan(22, 0, 0)
If timeNow >= Time1 And timeNow < Time2 Then
Return "1"
ElseIf timeNow >= Time2 And timeNow < Time3 Then
Return "2"
ElseIf timeNow >= Time3 And timeNow <= New TimeSpan(24, 0, 0) Or timeNow >= New TimeSpan(0, 0, 0) And timeNow < Time1 Then
Return "3"
End If
Return String.Empty
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.
-
Dec 17th, 2008, 08:21 AM
#4
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:
Public Enum Shift TenToSix SixToTwo TwoToTen End Enum
vb.net Code:
Dim currentShift As Shift Select Case Date.Now.Hour Case Is < 6, Is >= 22 currentShift = Shift.TenToSix Case Is < 14 currentShift = Shift.SixToTwo Case Else currentShift = Shift.TwoToTen End Select
-
Dec 17th, 2008, 08:39 AM
#5
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|