user14743092
user14743092

Reputation:

How to get the integer value of all days of week using DateTimeFormat.DayNames? c#

I'm using DateTimeFormatInfo.DayNames Property to get all day Names of a week , I would like to get the id of the days of week , I have seen many answers but I'm searching for a solution using this property .

How can I get the integer of all days of week ?

  days_in_arabic = new CultureInfo("ar-AE").DateTimeFormat.DayNames; 

//  this will return the days of week in arabic format.

Update:

My question now will be how can I get the index of the enum DayOfWeek to store it into an array of integer?

How can I get an array as an output like this :

[0,1,2,3,4,5,6]

and this array should be compatible with the days of week , I have already a Combobox that filled by an array of days , so I need to store the index of these days selected from the combobox into the database.

enter image description here

I have tested what he said in the comment and this what I got:

Day1 is Sunday
Day1 is Monday
Day1 is Tuesday
Day1 is Wednesday
Day1 is Thursday
Day1 is Friday
Day1 is Saturday
Day2 is Sunday
Day2 is Monday
Day2 is Tuesday
Day2 is Wednesday
Day2 is Thursday
Day2 is Friday
Day2 is Saturday

Upvotes: 0

Views: 602

Answers (2)

Arcord
Arcord

Reputation: 1909

If think the array you get will be in the same order as the "DayOfWeek" enum. So following the natural order and beginning with Sunday.

https://learn.microsoft.com/en-us/dotnet/api/system.dayofweek?view=net-5.0

Re-Edit : Proposed solution by @Enigmativity in comment:

Enum.GetValues(typeof(DayOfWeek)).Cast<int>().ToArray()

Upvotes: 1

Enigmativity
Enigmativity

Reputation: 117027

You can simply do this to get the values of the DayOfWeek enum:

int[] values = Enum.GetValues(typeof(DayOfWeek)).Cast<int>().ToArray();

Or:

int[] values = typeof(DayOfWeek).GetEnumValues().Cast<int>().ToArray();

Upvotes: -1

Related Questions