0

Good morning,

I am using a calendar form control to grab user selected dates and date ranges. On a calendar click event I refresh my form to show the user the selected start and end dates.

As part of the refresh, I update a text field to show the weekday and the date:

txtStartDate.Text = WeekdayName(Weekday(startDate)) + " " + startDate

However the weekday I am getting is 1 day out (Tuesday shows as Monday, Monday is showing as Sunday, and Sunday is showing as Saturday.)

To get around this I did the following:

txtStartDate.Text = WeekdayName(Weekday(startDate) - 1) + " " + startDate

This worked for Monday - Saturday, but when I select a Sunday on the calendar, it (obvious in hindsight!) crashes as it cannot display a date with value -1!

I have tried in vain to find a way to set the first day of the week as Monday instead of Sunday. I have done this to the calendar control but this only changes the displayed start day, not the value of the day. I have tried changing the CurrentCulture settings:

Thread.CurrentThread.CurrentCulture = New CultureInfo("en-UK")
Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-UK") 

This doesn't work either.

How can I set Monday as being day 0 in Weekday(startDate)?

DazEvans
  • 149
  • 4
  • 13
  • Why does it need to be a number? What would be wrong with calling the DayOfWeek property of a DateTime type. – Lotok May 20 '13 at 11:06

2 Answers2

3

The problem seems to be that Weekday defaults to Sunday being the first day of the week, while WeekdayName defaults to the current system default.

Try this:

txtStartDate.Text = WeekdayName(Weekday(startDate, FirstDayOfWeek.System)) + " " + startDate
Anders Lindahl
  • 41,582
  • 9
  • 89
  • 93
  • that works perfectly thank you ! I had been trying the firstdayofweek option but in slightly the wrong place.. doh! I do need the weekday in various places, so it could be more efficient to set the system wide firstdayofweek instead of each time I call the weekDayName method, if that is possible? – DazEvans May 20 '13 at 11:09
0

Try this:

Dim thisCulture As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture
Dim dayOfWeek As DayOfWeek = thisCulture.Calendar.GetDayOfWeek(startDate)
Dim dayName As String = thisCulture.DateTimeFormat.GetDayName(dayOfWeek)
SysDragon
  • 9,692
  • 15
  • 60
  • 89