DayOfWeek - get day number - vb.net

I have this method:
Dim dayofweek As Integer = CInt(DateTime.Now.DayOfWeek)
Dim startdayvalue As Integer = 0
Select Case args(1).ToLower
Case "monday"
startdayvalue = 1
Case "tuesday"
startdayvalue = 2
Case "wednesday"
startdayvalue = 3
Case "thursday"
startdayvalue = 4
Case "friday"
startdayvalue = 5
Case "saturday"
startdayvalue = 6
Case "sunday"
startdayvalue = 7
End Select
Return (dayofweek - startdayvalue + 7) Mod 7 + 1
args(1) is the a day like "monday" to "sunday" which the user can throw in. I want it to always start with value 1 for the given regional settings. I guess something is wrong with my last line because it gives me the wrong values. For example it gives me Monday = 1 (correct) Tuesday = 7 (incorrect) Sunday (2). In my regional settings week starts with Monday and should be one and Sunday 7.

You shouldn't try to work against the abstraction the .NET Framework supplies.
You can obtain the generic DayOfWeek number from your given culture:
Dim ci As System.Globalization.CultureInfo
ci = New System.Globalization.CultureInfo("de-DE")
Dim fdow As System.DayOfWeek
fdow = ci.DateTimeFormat.FirstDayOfWeek
You can use this DayOfWeek to compare against your DateTime.DayOfWeek to identify if this date is on the first day of the week for your culture.

Related

Select Case conversion not displaying properly, how can i fix?

I have an executable that when run takes an inputted date and then selects what date suffix is needed with it, however it is always defaulting to the case else.
when i have 02/04/2017 as format DD/MM/YYYY the result of the below case is 2th April instead of 2nd...
Could anyone enlighten me on the problem.
Dim Datewc As Date = Nothing
If CheckBox1.Checked Then
Datewc = TextBox1.Text
End If
'Determine date suffix
Dim datsuff As String = ""
Select Case CInt(Datewc.Day)
Case 1 Or 21 Or 31
datsuff = "st"
Case 2 Or 22
datsuff = "nd"
Case 3 Or 23
datsuff = "rd"
Case Else
datsuff = "th"
End Select
Parse you date
Dim dDate As DateTime =
DateTime.ParseExact(TextBox1.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)
Change your code a little, should work.
Day function returns integer, no need to Cast using CInt.
'Determine date suffix
Dim datsuff As String = ""
Select Case Datewc.Day
Case 1 , 21 , 31
datsuff = "st"
Case 2 , 22
datsuff = "nd"
Case 3 , 23
datsuff = "rd"
Case Else
datsuff = "th"
End Select
Explanation
Case 2 Or 22 means Case ((2 Or 22) ==true), It will be false hence going to else part.

offset for weekend days using mod

I have a .NET console app that needs to run on the 1st, 5th, 10th, 15th, 20th, and 25th of each month. Depending on which date it runs, the app sends out a different type of email. I want to adjust the app so that if one of those dates is on the weekend, it runs the following Monday.
I believe this does what I want:
Dim adjustedDay As Integer = Day(Today)
If Today.ToString("ddd") = "Mon" And adjustedDay > 1 Then
If adjustedDay Mod 5 = 1 Then
adjustedDay -= 1
ElseIf adjustedDay Mod 5 = 2 Then
adjustedDay -= 2
End If
End If
Select Case adjustedDay
Case 1
...
Case 5
...
Case ...
...
End Select
So if today is Monday, 9/21/2015, I adjust to "20" and run that code. If Monday happens to land on the 1st, I don't adjust it.
is there a cleaner way to do this?
Your code won't work correctly if Monday falls on the 2nd or 3rd of the month. (ie. 2 mod 5 = 2, so adjustedDay = 0)
The other tricky part is that the app needs to run on the 1st, but 1 mod 5 = 1 which is different to all the other cases where it is 0.
Something like this should work:
'Returns 0 for non-email days, otherwise returns 1,5,10,15,20,25
Private Function GetEmailDay(ByVal currentDate as Date) As Integer
If currentDate.DayOfWeek = DayOfWeek.Saturday OrElse
currentDate.DayOfWeek = DayOfWeek.Sunday Then
Return 0
End If
Dim day As Integer = currentDate.Day
Dim emailDays As New List(Of Integer)( {1,5,10,15,20,25} )
If emailDays.Contains(day) Then
Return day
End If
If currentDate.DayOfWeek = DayOfWeek.Monday Then
If emailDays.Contains(day-1) Then 'check Sunday
Return day-1
End If
If emailDays.Contains(day-2) Then 'check Saturday
Return day-2
End If
End If
Return 0
End Function

Getting Dates of Current week does not work as expected

iam trying to get the Dates (Monday - Sunday) for the current Week.
This is my current Code:
Dim kw As Integer = DatePart(DateInterval.WeekOfYear, Now, , FirstWeekOfYear.FirstFourDays)
If DatePart(DateInterval.Weekday, Now, Microsoft.VisualBasic.FirstDayOfWeek.Sunday) = 6 Then
kw = kw + 1
End If
Dim CurrDateFirstDay As Date = DateAdd(DateInterval.Day, 1, ReturnDateForWeekNumber(kw))
For i = 1 To 7
strCurrDay = FormatDateTime(CurrDateFirstDay, DateFormat.LongDate)
........
My Problem is that my code starts at 16.01.2013 and the last date is Thuesday 22.01.2013 next week. Why is that? Why does he start Wednesday 16.01.2013 and not Monday 14.01.2013? And why do i get returned dates of the next week? What iam doing wrong?
Edit:
ReturnDateForWeekNumber:
Public Shared Function ReturnDateForWeekNumber(ByVal iWeek As Integer) As DateTime
Return DateAdd(DateInterval.WeekOfYear, iWeek - 1, FirstDayOfYear)
End Function
What am I doing wrong?
You should step through your code in the debugger and observe the result, as I did:
Dim kw As Integer = DatePart(DateInterval.WeekOfYear, Now, , FirstWeekOfYear.FirstFourDays)
This returns the current week, which is week 3.
If DatePart(DateInterval.Weekday, Now, Microsoft.VisualBasic.FirstDayOfWeek.Sunday) = 6 Then
kw = kw + 1
End If
This checks if the weekday is the 6th day of the week (friday). We're not friday so If condition is not entered.
Dim CurrDateFirstDay As Date = DateAdd(DateInterval.Day, 1, ReturnDateForWeekNumber(kw))
This adds one day to the result of ReturnDateForWeekNumber, which returns:
Return DateAdd(DateInterval.WeekOfYear, iWeek - 1, FirstDayOfYear)
This adds 2 (week 3 minus 1) weeks to the first day of the year (Jan 1st), a tuesday. Jan 1st + 2 weeks = January 15th.
Now remember that you add one day to ReturnDateForWeekNumber, that's why CurrDateFirstDay has a value of January 16th.
Edit
I think your code is overly complicated and uses a lot of legacy VB6 functions. I would do it this way:
Dim myDate As Date = DateTime.Today
Dim dayDiff As Integer = myDate.DayOfWeek - DayOfWeek.Monday
Dim currentDay As Date = myDate.AddDays(-dayDiff) 'Monday
For i = 1 to 7
Console.WriteLine(currentDay)
'Do something with current day
currentDay = currentDay.AddDays(1)
Next
You might have to do some adjustments for your case but I believe this approach is simpler and less error prone.
Dim dateStartDateOfWeek As Date = GetWeekStartDate(52, 2014)
Dim dateEndDateOfWeek As Date = DateAdd(DateInterval.Day, 7, dateStartDateOfWeek)
Private Function GetWeekStartDate(ByVal weekNumber As Integer, ByVal year As Integer) As Date
Dim startDate As New DateTime(year, 1, 1)
Dim weekDate As DateTime = DateAdd(DateInterval.WeekOfYear, weekNumber - 1, startDate)
Return DateAdd(DateInterval.Day, (-weekDate.DayOfWeek) + 1, weekDate)
End Function

VB.NET - counting days between two dates with exclusions

I'm trying to count the days between two dates, excluding Saturdays and Sundays. I've written this code so far
Dim startDay As Integer
Dim endDay As Integer
Dim days As Integer
Dim count As Integer
startDay = dtpStartDate.Value.DayOfWeek
endDay = dtpEndDate.Value.DayOfWeek
For days = startDay To endDay
If days = 0 Or days = 6 Then 'Sunday = 0, Saturday = 6
count += 1
End If
Next
lblNoOfDays.Text = count
It works fine if you choose the two dates within the same week. (ex: 23rd Jan to 27th Jan, gives the result 5)
But if I set them to dates in different weeks, (ex : 23rd Jan to 30th Jan, gives the result 1), it gives incorrect results.
I know it happens because of the loop but I can't think of a way to overcome this. Can anyone give me a suggestion, solution??
Thank you
Dim count = 0
Dim totalDays = (dtpEndDate - dtpStartDate).Days
For i = 0 To totalDays
Dim weekday As DayOfWeek = startDate.AddDays(i).DayOfWeek
If weekday <> DayOfWeek.Saturday AndAlso weekday <> DayOfWeek.Sunday Then
count += 1
End If
Next
lblNoOfDays.Text = count
This function calculates the non-weekend days between two dates:
Public Shared Function WorkingDaysElapsed(ByVal pFromDate As Date, ByVal pToDate As Date) As Integer
Dim _elapsedDays As Integer = 0
Dim _weekendDays As DayOfWeek() = {DayOfWeek.Saturday, DayOfWeek.Sunday}
For i = 0 To (pToDate - pFromDate).Days
If Not _weekendDays.Contains(pFromDate.AddDays(i).DayOfWeek) Then _elapsedDays += 1
Next
Return _elapsedDays
End Function

subtract if the day of the week is a saturday or sunday

if the subtracted date is a saturday or sunday then subtract more days, before adding to arraylist
when i do this, the date stays the same and doesnt subtract, i get a conversion string to double error
Dim aftersubtraction As Date
Dim fromatafter As Date
aftersubtraction = departuredate.AddDays(-dates1.Text)
fromatafter = aftersubtraction.AddDays(-gracep.Text)
If fromatafter.DayOfWeek = "Saturday" Then
fromatafter.AddDays(-1)
dates.Add(fromatafter.ToString("MM/dd/yyyy"))
ElseIf fromatafter.DayOfWeek = "Sunday" Then
fromatafter.AddDays(-2)
dates.Add(fromatafter.ToString("MM/dd/yyyy"))
Else
dates.Add(fromatafter.ToString("MM/dd/yyyy"))
End If
While fromatafter.DayOfWeek = DayOfWeek.Saturday OrElse fromatafter.DayOfWeek = DayOfWeek.Sunday
fromatafter = fromatafter.AddDays(-1)
End While
dates.Add(fromatafter.ToString("MM/dd/yyyy"))
You have to assigne the date returned from AddDays to your variable.
And please set option strict and explicit. Why?
if im not mistaken the vb dayofweek function returns an integer and not a string.
' 0 = Sunday
' 1 = Monday
' 2 = Tuesday
' 3 = Wednesday
' 4 = Thursday
' 5 = Friday
' 6 = Saturday
hence the error message.
your getting the string to double error on the
line with the if statement... becase .dayofweek does not return a string... it returns an integer... so an integer is never going to be = "Saturday" or "Sunday"... its going to be equal to 0 or 1