09 July 2007

Get the corresponding day name from a date

In this article I'll discuss how to get the localized desciptive name of a day. Earlier I checked the daynumber and converted it to the description with a Select Case statement:

Dim d As Date = Date.Now
Dim s As String = Nothing

Select Case d.DayOfWeek
Case DayOfWeek.Monday
s = "Maandag"
Case DayOfWeek.Tuesday
s = "Dinsdag"

........
End Select


Get the non localized description

I'm writing this article on the 9th of july in the year 2007. This is on monday. The date class in the .NET framework has a property with which you can read the description of a day. This can be done with few lines of code:

Dim d As Date = Date.Now
Dim s As String = Nothing
s = d.DayOfWeek


The string variable s will now be filled with the value "Monday".

Get the localized description

To get the localized description you can use the following code:

Dim d As Date = Date.Now
Dim s As String = Nothing
Dim dtfi As System.Globalization.DateTimeFormatInfo = New System.Globalization.CultureInfo("nl-NL").DateTimeFormat
s = dtfi.GetDayName(d)

As you can see we localized a DateTimeFormatInfo. We're using this to GetDayName of the date. I used the dutch ISO countrycode "nl-NL" so the string will be populated with "Maandag".