Friday, April 29, 2011

Integer representation for day of the week

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?

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

    Lucero : To parse the XML date to a DateTime, I suggest using the XmlConvert class.
  • 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
    
  • (int)System.DateTime.Parse("2008-12-31T00:00:00.0000000+01:00").DayOfWeek + 1

  • (Int32)Convert.ToDateTime("2008-12-31T00:00:00.0000000+01:00").DayOfWeek + 1
    

0 comments:

Post a Comment