views:

506

answers:

4

I would like to convert a date object its integer representation for the day of week in C#. Right now, I am parsing a XML file in order to retrieve the date and storing that info in a string. It is in the following format:

"2008-12-31T00:00:00.0000000+01:00"

How can I take this and convert it into a number between 1 and 7 for the day of the week that it represents?

+6  A: 

If you load that into a DateTime varible, DateTime exposes an enum for the day of the week that you could cast to int.

Jason Coyne
To parse the XML date to a DateTime, I suggest using the XmlConvert class.
Lucero
+3  A: 
DateTime date = DateTime.Parse("2008-12-31T00:00:00.0000000+01:00");
int dayOfWeek = (int)date.DayOfWeek + 1; //DayOfWeek is 0 based, you wanted 1 based
Erv Walter
+3  A: 

(int)System.DateTime.Parse("2008-12-31T00:00:00.0000000+01:00").DayOfWeek + 1

pmarflee
+2  A: 
(Int32)Convert.ToDateTime("2008-12-31T00:00:00.0000000+01:00").DayOfWeek + 1
Daniel Brückner