I implmented a function to check time range in vb.net. But it is giving me wrong output.
My starttime is everyday at 11.00 Pm and end Time is 5.00 AM. My function if i pass 1.10 AM does not return me true output as this falls under that time range. Not sure what iam doing wrong.
Private Function CheckTimeRange() As Boolean
Dim retValue As Boolean = True
Try
Dim Dt As DateTime = DateTime.Now
Dim StartDt As DateTime = Convert.ToDateTime("11.00 PM")
Dim EndDt As DateTime = Convert.ToDateTime("5.00 AM")
Dim startTime As New TimeSpan(StartDt.Hour, StartDt.Minute, 0)
Dim endTime As New TimeSpan(EndDt.Hour, EndDt.Minute, 0)
Dim now As TimeSpan = DateTime.Now.TimeOfDay
If (now > startTime) AndAlso (now < endTime) Then
retValue = True
Else
retValue = False
End If
Return retValue
Catch ex As Exception
End Try
End Function
I think you're overcomplicating your code. You could do:
Private Function CheckTimeRange() As Boolean
Return DateTime.Now.Hour >= 23 OrElse DateTime.Now.Hour < 5
End Function
Edit:
If the start and end times are entered by the user, you should first convert the string values into TimeSpan objects, then you could use a more flexible method which takes date, min time and max time as parameters:
Private Function CheckTimeRange(myDate As DateTime, minTime as TimeSpan, maxTime As TimeSpan) As Boolean
If minTime > maxTime Then
Return myDate.TimeOfDay >= minTime OrElse myDate.TimeOfDay < maxTime
Else
Return myDate.TimeOfDay >= minTime AndAlso myDate.TimeOfDay < maxTime
End If
End Function
Example usage:
Dim minTime As New TimeSpan(23, 0, 0) 'Should be converted from 11.00 PM
Dim maxTime As New TimeSpan(5, 0, 0) 'Should be converted from 5.00 AM
CheckTimeRange(New Date(2012, 1, 1, 15, 0, 0), minTime, maxTime) '==> false
CheckTimeRange(New Date(2012, 1, 1, 22, 30, 0), minTime, maxTime) '==> false
CheckTimeRange(New Date(2012, 1, 1, 23, 00, 0), minTime, maxTime) '==> true
CheckTimeRange(New Date(2012, 1, 2, 1, 10, 0), minTime, maxTime) '==> true
CheckTimeRange(New Date(2012, 1, 2, 4, 59, 0), minTime, maxTime) '==> true
CheckTimeRange(New Date(2012, 1, 2, 5, 10, 0), minTime, maxTime) '==> false
If you have trouble converting string values to TimeSpan you should ask a new question for this specific task.
You're working with TimeSpan objects, and you'll find that 1.10 AM is never greater than 11.00pm without the date.
Meta-Knight has the better solution.
Related
I wish to change startDate depending on the day of the week. Here is what I've tried, the error I get is BC30311: Value of type 'Integer' cannot be converted to 'Date'.
Dim curr As Date = Date.Now
Dim day As String = Date.Today.DayOfWeek.ToString()
Dim startTime As New Date
If day="saturday" Then
startTime=(curr.Year, curr.Month, curr.Day, 17, 0, 0)
Else
startTime=(curr.Year, curr.Month, curr.Day, 13, 0, 0)
End If
Dim endTime As New Date(curr.Year, curr.Month, curr.Day, 21, 59, 0)
If (curr >= startTime) And (curr <= endTime) Then
'''Some Code
I simply need to change startDate to 13:00 hours for Saturdays. Any help appreciated
This is a better way to write code to do what you appear to want to do:
Dim currentDay = Date.Today
Dim startTime = currentDay.AddHours(If(currentDay.DayOfWeek = DayOfWeek.Saturday, 17, 13))
Dim endTime = currentDay.AddHours(21).AddMinutes(59)
Dim currentTime = Date.Now
If currentTime >= startTime AndAlso currentTime <= endTime Then
Hello I am new with datetimes in VB net and I am trying to calculate time for some operations in productions. For example:
starTtime="01.12.2018 07:00"
endTime ="01.12.2018 15:00"
I have example two periods without working:
Pause1Start="01.12.2018 10:00"
Pause1Stop="01.12.2018 10:30"
Pause2Start="01.12.2018 14:00"
Pause2Stop="01.12.2018 14:30"
How to calculate used time for operation?
It is not always this example, it is necessary to check is time for pause is included between two datetime or if there is end of working day, weekends, etc...
So operation can start in one day and finish day after.
I need idea how to work with this?
Thanks
Try this code. I suggest you to define lists of pause startings and endings, then loop through them, check if particular pause was during the working time and add that time in seconds to variable holding total seconds:
Sub Main()
Dim startTime = New DateTime(2018, 11, 1, 7, 0, 0)
Dim endTime = New DateTime(2018, 11, 1, 17, 0, 0)
Dim pauseStarts = New List(Of DateTime)
pauseStarts.Add(New DateTime(2018, 11, 1, 16, 0, 0))
pauseStarts.Add(New DateTime(2018, 11, 1, 10, 0, 0))
Dim pauseEnds = New List(Of DateTime)
' Add respective pause endings
pauseEnds.Add(New DateTime(2018, 11, 1, 18, 0, 0))
pauseEnds.Add(New DateTime(2018, 11, 1, 11, 30, 0))
Dim pauseSeconds As Long = 0
For i = 0 To pauseStarts.Count - 1
If pauseStarts(i) < endTime AndAlso pauseEnds(i) > startTime Then
' get the minimum from two times
Dim starting = If(startTime > pauseStarts(i), startTime, pauseStarts(i))
' get maximum from two times
Dim ending = If(endTime < pauseEnds(i), endTime, pauseEnds(i))
' add seconds during pause
pauseSeconds += (ending - starting).TotalSeconds
End If
Next
End Sub
I have an array dat that shows Type = Variant/Variant(0 to 500, 0 to 0, 0 to 1)
There is a "column" of dates:
dat(0, 0, 0) = #1/1/2013#
dat(1, 0, 0) = #1/2/2013#
I want to extract this set of dates. I tried:
Dim dat As Variant
Dim dt As Variant
'stuff gets dat in the format described above
dt = Application.Index(dat, 0, 1, 1)
Unfortunately this gives me an Error 13 Type Mismatch. What am I doing wrong?
Use a Loop
Sub dural()
Dim dat(0 To 500, 0 To 1, 0 To 1) As Variant
dat(0, 0, 0) = #1/1/2013#
dat(1, 0, 0) = #1/2/2013#
Dim dt(0 To 500) As Variant
For i = 0 To 500
dt(i) = dat(i, 0, 0)
Next i
End Sub
How to find week's starting date between two dates.
Example:
date1: 4th March 2014.
date2: 18th March 2014.
Then it should return list of weeks begining date as
2014-03-03, 2014-03-10, 2014-03-17
So first you want to get the first day of the week of the start-date? This gives your desired list:
Dim firstDate = New Date(2014, 3, 4)
Dim daysToFirstDay As Int32 = DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek - firstDate.DayOfWeek
firstDate = firstDate.AddDays(daysToFirstDay) ' -1 day in this case
Dim lastDate = New Date(2014, 3, 17)
Dim days As Int32 = (lastDate - firstDate).Days + 1 ' including last
Dim result As List(Of Date) = Enumerable.Range(0, days).
Select(Function(d) firstDate.AddDays(d)).
Where(Function(day) day.DayOfWeek = DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek).
ToList()
It's using LINQ to create a range of Dates between the start-and end-date. Then it uses the current DateTimeFormatInfo's FirstDayOfWeek property to return only week-starting days.
Function GetWeekStartDates(startDate As Date, endDate As Date) As List(Of Date)
Dim result As New List(Of Date)
Dim checkDate = startDate
Dim Is1stWeek As Boolean = True
Do While checkDate <= endDate
If checkDate.DayOfWeek = DayOfWeek.Monday Then
result.Add(checkDate)
If Is1stWeek Then Is1stWeek = False
Else
If Is1stWeek Then
result.Add(checkDate.AddDays(-checkDate.DayOfWeek + 1))
Is1stWeek = False
End If
End If
checkDate = checkDate.AddDays(1)
Loop
Return result
End Function
Can anyone help me on how can I validate given DateTime between two DateTime using vb.net?
the given DateTime must not in between the two DateTime..
You can compare date instances the same way you would compare numbers as they override the LessThan, LessThanOrEqual, GreaterThan and GreaterThanOrEqual operators:
Dim minDate = New DateTime(2009, 4, 17)
Dim maxDate = New DateTime(2011, 4, 17)
Dim selectedDate = New DateTime(2010, 4, 17)
If selectedDate > minDate And selectedDate < maxDate Then
Console.WriteLine("selected date is valid")
Else
Console.WriteLine("selected date is invalid")
End If
Use DateTime.Compare
Dim date1 As New DateTime(100)
Dim date2 As New DateTime(200)
Dim workingDate As New DateTime(150)
If DateTime.Compare(workingDate , date1) >= 0 AND DateTime.Compare(workingDate , date2) <= 0 Then
Console.WriteLine("Between")
Else
Console.WriteLine("Not Between")
End If
Dim dt As Date = DateTimePicker1.Value.Date
If dt < CDate("2010-04-01") Or dt > CDate("2010-04-10") Then
MessageBox.Show("Date is not in the first 10 days of April so is ok")
End If