Integer representation for day of the week

13,519

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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

Share:
13,519
THE DOCTOR
Author by

THE DOCTOR

I am a Time-Lord from the planet Gallifrey and the last remaining survivor of my race. I have regenerated several times now as follows: Database Developer -> QA Engineer -> Software Engineer -> Network/Systems Engineer -> Director of IT & Network Engineering.

Updated on June 27, 2022

Comments

  • THE DOCTOR
    THE DOCTOR almost 2 years

    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?