1

I have this method which shall return the day of the week (Wed):

protected string GetDayOfWeek(string dateTimeString)
{
    DateTime result = DateTime.Parse(dateTimeString);
    string dayOfWeek = Enum.GetName(typeof(DayOfWeek), result.DayOfWeek);

    return dayOfWeek;    
}

I have a breakpoint on the line DateTime result to check the incoming string which gives:

"Wed, 12 Mar 2014 00:00:00 GMT"

The above method is giving me error:

"FormatException not handled by the user code" String not recognised as a valid dateTime.

What am I doing wrong? I cannot pick it up.

Christos
  • 53,228
  • 8
  • 76
  • 108
Nullbyte
  • 231
  • 1
  • 6
  • 16
  • With a little search, I found this answer: http://stackoverflow.com/a/919276/1439453 Check if this is what you want. And if not, I sure there is a lot of "convert string to dateTime" topic and solutions. – provençal le breton Mar 12 '14 at 14:46
  • You probably want to use [DateTime.ParseExact](http://msdn.microsoft.com/library/w2sa9yss.aspx) with an appropriate [format string](http://msdn.microsoft.com/library/8kb3ddd4.aspx). – Corak Mar 12 '14 at 14:49

4 Answers4

4

DateTime.Parse(string) uses the conventions of the current culture. So my guess is that "Wed, 12 Mar 2014 00:00:00 GMT" is not a valid date in your current culture.

You could try:

DateTime.Parse(dateTimeString, System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat)

That should tell the parser to be culture independent.

Tor-Erik
  • 990
  • 8
  • 25
  • You got it right! The problem is the the current culture. Thanks a lot. Your answer lead to success. – Nullbyte Mar 12 '14 at 14:56
1

Try using the method DateTime.ParseExact and provide an exact format string for your input. In your case the call shall look like:

DateTime myDate = DateTime.ParseExact("Wed, 12 Mar 2014 00:00:00 GMT", "ddd, dd MMM yyyy HH:mm:ss 'GMT'K", System.Globalization.CultureInfo.InvariantCulture);
Tomas Walek
  • 2,516
  • 2
  • 23
  • 37
  • Thanks. Could you please show me how you would modify the above code. If instead of parse I use ParseExact, does not compile. – Nullbyte Mar 12 '14 at 14:49
  • @Nullbyte ParseExtract accepts 3 overloads. That's why it's not compiling in your code. http://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.90).aspx – Nathan Mar 12 '14 at 14:54
1

You can use:

DateTime.Now.DayOfWeek;

And compare with DayOfWeekclass.

h_s
  • 94
  • 4
0

There's two things going on here:

  1. Parse a string into a DateTime
  2. Get the DayOfWeek from the provided DateTime

As Tor-Erik suggests you can use DateTime.Parse with the invariant culture to get a DateTime.

DateTime.Parse(
    dateTimeString,
    System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat)

Then as Hevilávio Soares suggests you should use the built in functions of the DateTime object to obtain the Day of the week.

DateTime.Now.DayOfWeek;

It's better to separate the concerns and to reuse existing functionality than to write your own.

Kevin Hogg
  • 1,771
  • 25
  • 34