Click here to Skip to main content
15,867,488 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Rearrange day of week and capitalize day names in .Net

Rate me:
Please Sign up or sign in to vote.
4.33/5 (2 votes)
1 Jun 2010CPOL 10.2K   1  
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).


VB
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).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Croatia Croatia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --