Search code examples
c#dayofweek

How to get from an integer day value day as string


I stored into my database as integer day values. From a string array I was getting the day as string.

public static string[] _dayofweek = new string[] {"SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY"}; 

However, now I want to get the name of day relatively to cultureinfo not only as fixed in english.

Is it possible to get the names of week from this stored day number ?


Solution

  • The easiest way is to use ToString("dddd") on a DateTime. I would pick an arbitrary Sunday and use AddDays to translate your index.

    int _dayOfWeek = 1; //Monday
    string weekdayString = new DateTime(2015, 4, 12).AddDays(_dayOfWeek)
                                                    .ToString("dddd");
                         // ^ "Monday" or whatever locale you are in