Rearrange day of week and capitalize day names in .Net






4.33/5 (2 votes)
Zero-index will return monday (or any other day based on FirstDayOfWeek in culture)
As you might know CultureInfo will return day names from Sunday to Saturday. We in Europe start a week with Monday.
So I wrote a tiny function that will return day names in European style, and when you will request DayOfName(0) it will return Monday, instead of Sunday.
Also function will return capitalized name of day, in case you are just listing days and don't use them in context (when one should write day name lower case in some languages).
Public Function NameOfDay(ByVal dayOfWeek As Integer) As String
' Offset because of first day of week
Dim CurrentDay As Integer = (CurrentCulture.DateTimeFormat.FirstDayOfWeek + dayOfWeek) Mod 7
' Format name to have first letter uppercase
Return CurrentCulture.DateTimeFormat.DayNames(CurrentDay).Substring(0, 1).ToUpper & CurrentCulture.DateTimeFormat.DayNames(CurrentDay).Substring(1)
End Function
Above function does not rearrange day names inside culture info (which could be done) instead just temporary rearranges it in memory and returnes proper day name starting from Monday (for index 0) and finishing on Sunday (for index 6).