views:

276

answers:

3

hello my master!

i try to learn or generate any codes to learn string day("26.02.2009") ---> give me "wednesday"

i need static datefunction in C# .

Forexample:

datetime Str_day= Returnstringdate("09.02.2009"); ---->Str_day="Monday";

Returnstringdate("09.02.2009")

{ it must return Monday!!! }

OR

Returnstringdate("09.02.2009 12:30:32")

{ it must return Monday!!! }

+7  A: 

DateTime.ParseExact allows you to specify the exact format of the date you are parsing. You can then use ToString("dddd") to return the day of the week as a string.

DateTime date = DateTime.ParseExact("09.02.2009", "dd.MM.yyyy", CultureInfo.InvariantCulture);

string dayOfWeek = date.ToString("dddd");

Alternatively you can use the DayOfWeek property, which returns a System.DayOfWeek enumeration value.

DateTime date = DateTime.ParseExact("09.02.2009", "dd.MM.yyyy", CultureInfo.InvariantCulture);

DayOfWeek day = date.DayOfWeek;
string dayString = day.ToString("G");
Richard Szalay
Note that DateTime.ParseExact requires a third IFormatProvider parameter.
Tyalis
Added, thanks for the heads up.
Richard Szalay
+2  A: 
    DayOfWeek day = DateTime.ParseExact(
        "26.02.2009", "dd.MM.yyyy", CultureInfo.InvariantCulture).DayOfWeek;
    string dayString = day.ToString();
Marc Gravell
Thanks Marc . You may genious!!!
Phsika
A: 

Use the DateTime.DayOfWeek Property on your DateTime object.

Also see this thread.

Cerebrus