10

I was wondering if there was a way to directly convert the integer DayOfWeek returns into a string representing the day like Monday, Tuesday etc.

Sample code:

MessageBox.Show(Date.Today.DayOfWeek)

This will return 6 (as of today). Is there a way to directly convert this into Saturday, for example? I don't really care what it really converts it into, but I want to do away with my Select Case:

Select Case Date.Today.DayOfWeek
     Case 0
         day = "Sunday"
     Case 1
         day = "Monday"
     Case 2
         day = "Tuesday"
     Case 3
         day = "Wednesday"
     Case 4
         day = "Thursday"
     Case 5
         day = "Friday"
     Case 6
         day = "Saturday"
     Case Else
         day = "Apocalypse: we're all boned."
 End Select

Thanks :)

furkle
  • 5,019
  • 1
  • 15
  • 24
James
  • 139
  • 2
  • 3
  • 7
  • I don't think that the .NET framework supports your `Case Else`. Too bad... – Thomas Jan 30 '10 at 19:32
  • Probably something MS should look at... it'll happen one day. Although I do get errors using `day` later on in the code if it's not there :P – James Jan 30 '10 at 19:35
  • Nothing is wrong with using `Case Else`. There are easier ways to get day name. However as Jon pointed out, DayOfWeek is an enum type, so it will never be anything other than 0 to 6. – CoderDennis Jan 30 '10 at 19:41
  • And just to emphasize what has also been been said by others, Sunday is 0, not 7. http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx – RenniePet Sep 20 '13 at 15:24

6 Answers6

13

DateTimeFormatInfo.CurrentInfo.GetDayName.

itowlson
  • 73,686
  • 17
  • 161
  • 157
5

A simpler way:

Dim day As String = Date.Today.DayOfWeek.ToString()
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • @ghord this technique just transform an enumeration to a string value; you probably would do something like `DateTime.Today.ToString("dddd", new CultureInfo("pt-br"))` – Rubens Farias Nov 14 '14 at 20:21
2

There's a DateTime format for that: dddd

Dim date1 As Date = #08/29/2008 7:27:15PM#
date1.ToString("dddd", CultureInfo.CreateSpecificCulture("en-US"))

With the CultureInfo you can get it in a specific language (it's optional)

For more info: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#ddddSpecifier

Yvo
  • 18,681
  • 11
  • 71
  • 90
0

DateTime.DayOfWeek doesn't return an integer - it returns an enum of type DayOfWeek. I'd expect that to be converted into the name automatically, but maybe this is a VB subtlety; maybe something to do with using Date instead of DateTime? Try this:

MessageBox.Show(DateTime.Today.DayOfWeek.ToString())

This won't be culture-sensitive though - it will always just display the name of enum value, in English. If that's not good for you, use Zyphrax or itowlson's solution.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • This will return the invariant (US English) name; fine for debugging or storing in a database or whatever, but if he's displaying it he should be using something that localises the string. – itowlson Jan 30 '10 at 19:37
  • @itowlson: I was just editing to say something similar, but less specifically. Not *every* UI needs to be localised, IMO. There are plenty of apps which will only ever be in one language (think internal apps for non-international companies). If that language is English, that makes life even easier. – Jon Skeet Jan 30 '10 at 19:38
  • Thanks. And this code is just to reference some TextBoxes in my form in a timetable order. The actual data going to be displayed is coming from a database :) – James Jan 30 '10 at 19:49
0

Date.Today.DayOfWeek.ToString will give you what you're looking for. Nice and easy.

CoderDennis
  • 13,642
  • 9
  • 69
  • 105
-2

Just in case others looked at this example. I believe it should be Case 0 for Sunday.

Case 0 
    day = "Sunday"
Greg Gage
  • 1
  • 1
  • What you have here is a comment on the question, not an answer. Posting comments as answer to circumvent the rules inevitably leads to moderator intervention. – Louis Apr 14 '14 at 00:16
  • Sounds good. But I searched for this example code, and it was incorrect beyond the question. I spent some time figuring it out. I think it should be pointed out for future users, even it the question was already answered. – Greg Gage Apr 14 '14 at 00:19