Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given a string with day of week, return integer

Tags:

c#

.net

Is there a way to have something like:

string day = "Sunday";
int num = getDayOfWeek(day); //returns 0

I understand we could something like, and I wanted the reverse:

int num = 0;

//returns "Sunday"
return System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames[(int)num]; 

The easiest way would probably be to implement a dictionary that does what I am asking, but I wonder if there is something in C# DateTime functions that already does so for me.

EDIT:

As Jon Skeet pointed out, it would be ideal that the answer supported different cultures days (e.g. "Sunday" in English, "Segunda" in Portuguese...)

like image 213
Manuel Reis Avatar asked Dec 05 '22 02:12

Manuel Reis


1 Answers

var inx = Array.FindIndex(CultureInfo.CurrentCulture.DateTimeFormat.DayNames, x=>x=="Sunday");
like image 52
EZI Avatar answered Dec 23 '22 09:12

EZI