I am trying to implement a method in VB.NET that could subtract a start datetime and datetime, and result this specific string period "D.HH:mm:ss", observing that the day doesn't have 24 hours, but only 8 hours.
My function to do the subtract return only the diff in hours and as decimal:
Public Function WH(ByVal date1 As Date, ByVal date2 As Date,
Optional ByVal considerwk As Boolean = True) As Decimal
Dim ini_tim As DateTime = DateTime.Parse("08:00")
Dim end_tim As DateTime = DateTime.Parse("18:00")
'//RESULT
Dim _res As Integer
'//WHILE INITIAL LESS THAN END...
Do While date1 <= date2
'//INSIDE INTERVAL?
If Hour(date1) >= Hour(ini_tim) And Hour(date1) <= Hour(end_tim) Then
'//CONSIDER WORKDAY?
If considerwk = True Then
'//IF NOT SATURDAY OR SUNDAY
If Weekday(date1) <> vbSaturday And Weekday(date1) <> vbSunday Then
'//ADD +1 IN RESULT
_res += 1
End If
Else
'//ADD +1 IN RESULT
_res += 1
End If
End If
'//ADD A MINUTE IN THE DATE
date1 = DateAdd("n", 1, date1)
Loop
'//RETURN THE DIFF IN DEC
Return CDec(_res / 60)
End Function
Hope that you can help me!
Thanks!
Something to consider...
With your current code:
Hour(date1) <= Hour(end_tim)
A time of 18:01 would be considered "inside the interval" when it's actually after the end time!
Try something more like below. It'll be accurate down to the second and can handle it if your start/stop times are not exactly at the top of the hour (like 08:30 to 18:30):
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dt1 As DateTime = DateTime.Now
Dim dt2 As DateTime = dt1.AddDays(5).AddHours(4).AddMinutes(39).AddSeconds(17)
Dim ts As TimeSpan = WH(dt1, dt2, True)
Dim strFormattedTotal As String = String.Format("{0}.{1}:{2}:{3}", ts.Days, ts.Hours.ToString("00"), ts.Minutes.ToString("00"), ts.Seconds.ToString("00"))
Label1.Text = "Total: " & strFormattedTotal
Label2.Text = "Total Hours: " & ts.TotalHours
End Sub
Public Function WH(ByVal date1 As Date, ByVal date2 As Date, Optional ByVal considerwk As Boolean = True) As TimeSpan
Static ini_tim As DateTime = DateTime.Parse("08:00")
Static end_tim As DateTime = DateTime.Parse("18:00")
Dim TS As New TimeSpan
Do While date1 < date2
If date1.TimeOfDay >= ini_tim.TimeOfDay AndAlso date1.TimeOfDay <= end_tim.TimeOfDay Then
If considerwk Then
If date1.DayOfWeek <> DayOfWeek.Saturday AndAlso date1.DayOfWeek <> DayOfWeek.Sunday Then
TS = TS.Add(TimeSpan.FromSeconds(1))
End If
Else
TS = TS.Add(TimeSpan.FromSeconds(1))
End If
End If
date1 = date1.AddSeconds(1)
Loop
Return TS
End Function
Related
I am trying to find the fourth Wednesday of the current Month and Year when a form loads
I have converted numerous C# code to Visual Basic with no results
Would someone explain what is wrong with this code OR explain how to accomplish this in VB Code
Private Sub SurroundingSub()
Dim thisDate As Date = Today
tbMonth.Text = thisDate.ToString("MMMM")
tbYear.Text = thisDate.ToString("yyyy")
Dim month As Integer
month = tbMonth.Text
Dim year As Integer
year = tbYear.Text
Dim fourthWed As Date = New Date(year, month, 4)
tbFour.Text = fourthWed.ToLongDateString
While fourthWed.DayOfWeek <> DayOfWeek.Wednesday
fourthWed = fourthWed.AddDays(1)
tbFour.Text = fourthWed.ToShortDateString
End While
End Sub
This is a JavaFX statement that I am trying to implement in VB.Net
if(TorF.equals("F") && today.isAfter(fourthTUEofMONTH))
This sets the date
public void setDATES() throws IOException, SQLException{
today = LocalDate.now();
fourthTUEofMONTH = LocalDate.now();
fourthTUEofMONTH = fourthTUEofMONTH.with(TemporalAdjusters.dayOfWeekInMonth(4, DayOfWeek.TUESDAY));
endMONTH = LocalDate.now();
endMONTH = endMONTH.with(TemporalAdjusters.lastDayOfMonth());
}
For a more general solution, with my function you can easily find the first, second, third, forth or fifth sunday, monday, tuesday, wednesday or whatever you want:
Public Function GetXDayOfWeek(Day As DateTime,
DayOfWeek As DayOfWeek,
Optional Index As Integer = 1) As DateTime
Dim First As New Date(Day.Year, Day.Month, 1)
Dim Diff As Integer = (DayOfWeek - First.DayOfWeek + 7) Mod 7
Return First.AddDays(Diff + (Index - 1) * 7)
End Function
So if you want to find the forth wednesday of the current month, use it like:
Dim DateToFind As DateTime = GetXDayOfWeek(Today, DayOfWeek.Wednesday, 4)
Your while statement will stop on the first Wednesday it finds, not the fourth. Keep track of the number of Wednesdays you encounter as you iterate and once you find the fourth then you can update tbFour.
Also as mentioned in the comments you'll want to start at the first day of the year.
Dim fourthWed As Date = New Date(year, month, 1)
Dim wednesdayCursor As Integer = 0
While wednesdayCursor < 4
If fourthWed.DayOfWeek = DayOfWeek.Wednesday Then
wednesdayCursor += 1
End If
fourthWed = fourthWed.AddDays(1)
End While
'Subtract one day because we added one on loop:
fbFour.Text = fourthWed.AddDays(-1).ToShortDateString()
You should make the function of getting the fourth Wednesday into a separate method, perhaps generalizing it for any day of the week, but just for the fourth Wednesday...
Module Module1
Function FourthWedOfMonth(dt As DateTime) As Integer
Dim currDate = New DateTime(dt.Year, dt.Month, 1)
Dim nWednesdays = 0
While nWednesdays < 4
If currDate.DayOfWeek = DayOfWeek.Wednesday Then
nWednesdays += 1
End If
currDate = currDate.AddDays(1)
End While
Return currDate.Day - 1
End Function
Sub Main()
For mo = 1 To 12
Console.Write(FourthWedOfMonth(New DateTime(2020, mo, 17)) & " ")
Next
Console.ReadLine()
End Sub
End Module
Outputs the correct days for 2020:
22 26 25 22 27 24 22 26 23 28 25 23
If you wanted the DateTime of the fourth Wednesday, you could
Function FourthWedOfMonth(dt As DateTime) As DateTime
Dim currDate = New DateTime(dt.Year, dt.Month, 1)
Dim nWednesdays = 0
While nWednesdays < 4
If currDate.DayOfWeek = DayOfWeek.Wednesday Then
nWednesdays += 1
End If
currDate = currDate.AddDays(1)
End While
Return New DateTime(dt.Year, dt.Month, currDate.Day - 1)
End Function
and then Console.WriteLine(FourthWedOfMonth(DateTime.Today).ToString("dd-MMM-yyyy")) would output "22-Jul-2020" (at the time of writing).
I'd find the fourth Wednesday of the current month this way:
Private Sub FourthWednesdayOfCurrentMonth()
Dim firstOfMonth As DateTime = DateTime.Today.AddDays(-1 * (DateTime.Today.Day - 1))
Dim firstWednesday As DateTime = firstOfMonth.AddDays((7 + (DayOfWeek.Wednesday - firstOfMonth.DayOfWeek)) Mod 7)
Dim fourthWednesday As DateTime = firstWednesday.AddDays(21)
tbYear.Text = fourthWednesday.Year
tbMonth.Text = fourthWednesday.Month
tbFour.Text = fourthWednesday.Day
End Sub
Written generically for any day of the week, that would change to:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim fourthWednesday As DateTime = FourthWeekDayOfCurrentMonth(DayOfWeek.Wednesday)
tbYear.Text = fourthWednesday.Year
tbMonth.Text = fourthWednesday.Month
tbFour.Text = fourthWednesday.Day
End Sub
Private Function FourthWeekDayOfCurrentMonth(ByVal WeekDay As DayOfWeek) As DateTime
Dim firstOfMonth As DateTime = DateTime.Today.AddDays(-1 * (DateTime.Today.Day - 1))
Dim firstWeekday As DateTime = firstOfMonth.AddDays((7 + (WeekDay - firstOfMonth.DayOfWeek)) Mod 7)
Return firstWeekday.AddDays(21)
End Function
Its quite simple, i just want to compare two dates using month and year, if the input date (mont and year only) are above or below that current date (month and year).
The problem is , when i compare two strings
Dim dDate as DateTime
If Not (DateTime.TryParse(txtBox.Text, dDate)) Then
MessageBox.Show("check date.")
Else
txtBox.Text = dDate.ToString("MM/yyyy")
end If
IF dDate.ToString("MM/yyyy") < DateTime.Now.ToString("MM/yyyy")
MessageBox.Show("Below") ' Problem: 03/2024 saying is below than 08/2019
Else
MessageBox.Show("Above")
End If
Any help?
UPDATE
I CHANGED THE CASE TO
If (dDate.Month AndAlso dDate.Year) < (DateTime.Now.Month AndAlso DateTime.Now.Year) Then
'input: 07/2019
'date expired
Else
'the problem is here
'saying 07/2019 is not < than 08/2019
End If
I'd avoid using strings.
Dim dDate As DateTime
If Not (DateTime.TryParse(txtBox.Text, dDate)) Then
'bad date
MessageBox.Show("check date.")
Else
Select Case dDate.Year
Case Is < DateTime.Now.Year
MessageBox.Show("Below")
Case Is > DateTime.Now.Year
MessageBox.Show("Above")
Case Else 'years are equal,check month
Select Case dDate.Month
Case Is < DateTime.Now.Month
MessageBox.Show("Below")
Case Is > DateTime.Now.Month
MessageBox.Show("Above")
Case Else 'equal months
MessageBox.Show("SAME") '????
End Select
End Select
End If
Using date values is probably best, but if string comparisons must be made.
Dim dDate as DateTime
If Not (DateTime.TryParse(txtBox.Text, dDate)) Then
MessageBox.Show("check date.")
Else
txtBox.Text = dDate.ToString("yyyyMM")
End If
If dDate.ToString("yyyyMM") < DateTime.Now.ToString("yyyyMM") Then
MessageBox.Show("Below")
ElseIf dDate.ToString("yyyyMM") > DateTime.Now.ToString("yyyyMM") Then
MessageBox.Show("Above")
Else
MessageBox.Show("Same")
End If
Thank you everyone, but the final solutions is made with
If (dDate.Year < DateTime.Now.Year Or (dDate.Year = DateTime.Now.Year And dDate.Month < DateTime.Now.Month)) Then
'something
Else
'something
End If
Convert the year and month to Integer values, which are easy to compare less-than or greater-than. For example, if you had two DateTimePicker controls on a form, named dtpDate1 and dptDate2, and a button btnTest:
Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
Dim date1 As Integer = (dtpDate1.Value.Year * 100) + dtpDate1.Value.Month
Dim date2 As Integer = (dtpDate2.Value.Year * 100) + dtpDate2.Value.Month
MsgBox("Date1 = " & date1.ToString & vbCrLf & "Date2 = " & date2.ToString)
If date1 = date2 Then MsgBox("Equal!")
If date1 < date2 Then MsgBox("date1 is less than date2")
If date1 > date2 Then MsgBox("date1 is greater than date2")
End Sub
The dates are converted to integers in a YYYYMM format. This is a data warehouse technique to convert dates to integers for better query performance when date is used often in a WHERE clause.
I have a code that gets an ETA (estimated time of arrival) but I want it to exclude weekends. I also have it to change the ETA if its past 2:30PM.
Code:
Dim ETA1 As Date = Date.Today.AddDays(1)
Dim ETA2 As Date = Date.Today.AddDays(2)
Dim ETA3 As Date = Date.Today.AddDays(3)
Dim day As String = Format(Today, "dddd")
Dim time As Date
Dim CurrHour As Integer
Dim CurrMinute As Integer
time = DateTime.Now
CurrHour = time.Hour
CurrMinute = time.Minute
If StoreBox.Text Like "25*" Then
MicroLabel.Visible = True
If CurrHour >= 2 AndAlso CurrMinute >= 30 Then
ETABox.Text = ETA2
Else
ETABox.Text = ETA1
End If
Else
MicroLabel.Visible = False
If CurrHour >= 2 AndAlso CurrMinute >= 30 Then
ETABox.Text = ETA2
Else
ETABox.Text = ETA1
End If
End If
DateTime is a very flexible type which allows you to easily perform many date/time related actions. You don't need to perform a string-like analysis (what your code is doing).
For example, to take care of the two requested functionalities, just do something like:
Dim curTime As DateTime = Now
Dim goAhead As Boolean = True
If curTime.DayOfWeek = DayOfWeek.Saturday OrElse curTime.DayOfWeek = DayOfWeek.Sunday Then
goAhead = False
ElseIf curTime > New DateTime(curTime.Year, curTime.Month, curTime.Day, 14, 30, 0) Then
goAhead = False
End If
If goAhead Then
'Weekday before 2:30 PM
End If
Given today's date want to get the date of the each Friday for the last four weeks.
Here is an easy LINQ approach:
Dim today = Date.Today
Dim lastFridays = From d In Enumerable.Range(0, Int32.MaxValue)
Let dt = today.AddDays(-d)
Where dt.DayOfWeek = DayOfWeek.Friday
Select dt
Dim lastFourFridays As Date() = lastFridays.Take(4).ToArray()
Since it's not the most efficient approach, here is a query that is still readable and maintainable but only searches the first friday and then takes only every 7th day:
Dim lastFriday = lastFridays.First() ' reuse of above query '
Dim fridays = From d In Enumerable.Range(0, Int32.MaxValue)
Let dt = lastFriday.AddDays(-d * 7)
Select dt
Dim lastFourFridays As Date() = fridays.Take(4).ToArray()
You may consume this one, which returns a list of such dates and excludes the one if the specifiedDate date is Friday:
Public Shared Function GetLastFourFridays(specifiedDate As DateTime) As List(Of DateTime)
Dim dtm As New List(Of DateTime)()
Dim dt As DateTime = specifiedDate
For i As Integer = 0 To 6
dt = dt.AddDays(-1)
If dt.DayOfWeek = DayOfWeek.Friday Then
dtm.Add(dt)
Exit For
End If
Next
dtm.Add(dt.AddDays(-7))
dtm.Add(dt.AddDays(-14))
dtm.Add(dt.AddDays(-21))
Return dtm
End Function
and the way you use it is:
Dim dtm As List(Of DateTime) = GetLastFourFridays(DateTime.Now)
For Each d As var In dtm
Console.WriteLine(String.Format("Date: {0}, Day: {1}", d.ToString(), [Enum].Parse(GetType(DayOfWeek), d.DayOfWeek.ToString())))
Next
Here is my way:
Function Last4Friday(ByVal StartDate As Date) As array
Dim L4F()
Dim mDate as date = StartDate
For value As Integer = 1 To 7
mDate = mDate.AddDays(-1)
If mDate.DayOfWeek = DayOfWeek.Friday Then
L4F = {mDate, mDate.AddDays(-7), mDate.AddDays(-14), mDate.AddDays(-21)}
exit for
End If
Next
Return L4F
End Function
Edit: If you need to check the inserted date and you want it returned in the array you may simply use:
Dim mDate as date = StartDate.AddDays(1)
instead of
Dim mDate as date = StartDate
Try this. It doesn't use a loop to find the starting Friday.
Dim someDate As DateTime = DateTime.Now
If someDate.DayOfWeek <> DayOfWeek.Friday Then
'do the math to get a Friday
someDate = someDate.AddDays(DayOfWeek.Friday - someDate.AddDays(1).DayOfWeek - 6)
End If
Dim last4Fridays As New List(Of DateTime) From {someDate, someDate.AddDays(-7), someDate.AddDays(-14), someDate.AddDays(-21)}
All of the other suggestions have used a loop to find the starting Friday. If this code is used infrequently then how the starting Friday is determined might not matter.
edit: as function
Function FindLastFourFridays(someDate As DateTime) As List(Of DateTime)
'Find first Friday to include
If someDate.DayOfWeek <> DayOfWeek.Friday Then
someDate = someDate.AddDays(DayOfWeek.Friday - someDate.AddDays(1).DayOfWeek - 6)
' uncomment these two lines if you do not want initial someDate.DayOfWeek = DayOfWeek.Friday to be included
'Else
' someDate = someDate.AddDays(-7)
End If
'build the return (four fridays)
Dim last4Fridays As New List(Of DateTime) From {someDate, someDate.AddDays(-7), someDate.AddDays(-14), someDate.AddDays(-21)}
Return last4Fridays
End Function
This function does not need to be passed a date it picks up today's date and gets the last four Friday's from today. It can be changed around to get any day of the week.
Dim todaysDate As Date = Date.Today
Dim oldDay As Integer
Dim thisWeek As Date
Dim firstWeek As Date
Dim secondWeek As Date
Dim thirdWeek As Date
Dim fourthWeek As Date
'finds the Friday of the end of the current week No mattter what day you are working
Dim daycount As Integer
'use this to check specific dates "Dim datetime As New DateTime(2015, 4, 13)"
oldDay = Weekday(todaysDate)
thisWeek = todaysDate
If oldDay < 6 Then
daycount = 6 - oldDay
thisWeek = thisWeek.AddDays(+daycount)
ElseIf oldDay > 6 Then
daycount = oldDay - 6
thisWeek = thisWeek.AddDays(-daycount)
End If
Dim currentDate As Date = Now
Do While Not currentDate.DayOfWeek = DayOfWeek.Friday
currentDate = currentDate.AddDays(-1)
Loop
fourthWeek = currentDate.AddDays(-21)
thirdWeek = currentDate.AddDays(-14)
secondWeek = currentDate.AddDays(-7)
firstWeek = currentDate
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