32

I have converted several days of the week to their respective integer values..

For example: Tuesday, Thursday, Friday As 2,4,5

Now I need to get back to the days from the integers.

Simply the inverse of what i had done.

Inverse of the Question: Get Integer value of day of week

Is there a simple standard way for getting day of week from integer value in C# or else I will have to perform a manual calculation with a Method?

Community
  • 1
  • 1
parkourkarthik
  • 834
  • 1
  • 8
  • 20

6 Answers6

45

try below code :-

Response.Write(Enum.GetName(typeof(DayOfWeek),5));

Output:

Friday

and If you have to convert integers to days of week, see following sample to convert “2,4,5″ using LINQ.

var t = string.Join(",",
                 from g in "2,4,5".Split(new char[] { ',' })
                 select Enum.GetName(typeof(DayOfWeek), Convert.ToInt32(g)));
        Response.Write(t);

Output:

Tuesday,Thursday,Friday

For extra informations :-

http://msdn.microsoft.com/en-us/library/system.enum.getname(v=vs.110).aspx

Neel
  • 11,625
  • 3
  • 43
  • 61
23

Try

CultureInfo.CurrentCulture.DateTimeFormat.DayNames[day No]
Gumzle
  • 847
  • 6
  • 16
9

Adding my answer in case it's any use to anyone:

((DayOfWeek)i).ToString();

Gives 0 = Sunday, 1 = Monday etc

For 0 = Monday just shift along 1

((DayOfWeek)((i + 1) % 7)).ToString();
mbdavis
  • 3,861
  • 2
  • 22
  • 42
7
Enum.Parse(typeof(DayOfWeek),"0")

where "0" is string equivalent of integer value of day of the week

Manas
  • 2,534
  • 20
  • 21
2

In DateTime.Now DayOfWeek is an enum value and you can get its string value by parsing it to corresponding values.

Enum.Parse(typeof(DayofWeek),"0")

You will get your desired result then.

r0-
  • 2,388
  • 1
  • 24
  • 29
Chandra
  • 94
  • 1
  • 9
1
string.Format("{0:dddd}", value)

Using enumeration doesn't factor in localisation. This string format should return the full day name as a string localised to local culture.

class Program
{
    static void Main(string[] args)
    {
        var value = DateTime.Today;
        System.Console.WriteLine(string.Format("{0:dddd}", value));
    }
}

Console output on 11th of March 2020, locale en-GB:

Wednesday
kidshaw
  • 3,423
  • 2
  • 16
  • 28