+chorpeac MVC Posted June 3, 2004 MVC Share Posted June 3, 2004 Ok what is wrong with this... System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US", true); string dateStr = "Thu, 6 May 2004 18:08:13 -0400"; DateTime.ParseExact(dateStr,"ddd, dd MMM yyyy HH':'mm':'ss zzz",ci); Error: String was not recognized as a valid DateTime Link to comment Share on other sites More sharing options...
0 smurfiness Posted June 3, 2004 Share Posted June 3, 2004 I believe the US format for that date would be Thu, May 6. Thu, 6 May is the international English format. Try using just "en" as your culture string, or altering the date to a US format. Link to comment Share on other sites More sharing options...
0 +chorpeac MVC Posted June 3, 2004 Author MVC Share Posted June 3, 2004 When I change it to en, it says Culture "en" is a neutral culture. It can not be used in formatting and parsing and therefore cannot be set as the thread's current culture. Should I do it this way instead? DateTime.ParseExact(dateStr ,new string[]{"r","ddd, dd MMM yyyy HH':'mm':'ss zzz"},System.Globalization.DateTimeFormatInfo.InvariantInfo,System.Globalization.DateTimeStyles.None); Link to comment Share on other sites More sharing options...
0 +chorpeac MVC Posted June 3, 2004 Author MVC Share Posted June 3, 2004 This is the format I am receiving when I get date information from mime mail format. So the date format I posted is just what I am receiving from the mail, not my own format. Link to comment Share on other sites More sharing options...
0 +chorpeac MVC Posted June 3, 2004 Author MVC Share Posted June 3, 2004 no date experts out there? Link to comment Share on other sites More sharing options...
0 jh_newroom Posted June 3, 2004 Share Posted June 3, 2004 Just do this, it works fine try { string dateStr = "Thu, 6 May 2004 18:08:13 -0400"; DateTime dt = DateTime.Parse(dateStr); } catch ( Exception ex ) { System.Diagnostics.Debug.WriteLine( ex.Message ); } J. Link to comment Share on other sites More sharing options...
0 azcodemonkey Posted June 3, 2004 Share Posted June 3, 2004 (edited) Didn't work fine for me. Might be a bug, because that is a valid RFC1123 date format. Edited June 3, 2004 by weenur Link to comment Share on other sites More sharing options...
0 +chorpeac MVC Posted June 3, 2004 Author MVC Share Posted June 3, 2004 Gesh I looked at this and check it out. This was what I had to change... DateTime.ParseExact(dateStr ,new string[]{"r","ddd, d MMM yyyy HH':'mm':'ss zzz"},System.Globalization.DateTimeFormatInfo.InvariantInfo,System.Globalization.DateTimeStyles.None); changed the date from dd to d since it was returning a single number not a 06, but a 6 rather as the date. Link to comment Share on other sites More sharing options...
0 azcodemonkey Posted June 3, 2004 Share Posted June 3, 2004 Oh, brother! :s Link to comment Share on other sites More sharing options...
0 +chorpeac MVC Posted June 3, 2004 Author MVC Share Posted June 3, 2004 yeah no kidding.... Link to comment Share on other sites More sharing options...
0 Sn1p3t Posted June 6, 2004 Share Posted June 6, 2004 What was wrong with jh_newroom's suggestion? I ran both and it seemed to return the same DateTime, although CompareTo returned 0 (false). Maybe it's just the way it was formatted parsed? I suppose your method is better for all types of cultures. Link to comment Share on other sites More sharing options...
0 +chorpeac MVC Posted June 7, 2004 Author MVC Share Posted June 7, 2004 I quickly looked around to see what the difference was, and it just looks like parseexact requires a specific format... *shrug* Link to comment Share on other sites More sharing options...
0 +chorpeac MVC Posted June 30, 2004 Author MVC Share Posted June 30, 2004 Is there anyway to use Convert.ToDateTime on a string like this Thu, 6 May 2004 18:08:13 -0400? It works if the -0400 is not there, but when I put that there it throws and exception. The parseexact works on that string. However, I am not always positive that a date string is going to follow that format. So I was trying to get a more generic way to convert a string into a datetime object. Any ideas? Link to comment Share on other sites More sharing options...
0 jamend Posted June 30, 2004 Share Posted June 30, 2004 Since the -0400 style of timezones does not seem to be standard (and not generic), the best way I could think of was to just use Try...Catch. Link to comment Share on other sites More sharing options...
0 azcodemonkey Posted June 30, 2004 Share Posted June 30, 2004 (edited) If I could only read... :blush: Edited June 30, 2004 by weenur Link to comment Share on other sites More sharing options...
0 azcodemonkey Posted June 30, 2004 Share Posted June 30, 2004 Damned edit timers... :p Anyhow, you could write a method that contained the formats you want, plus all the other formats. You may also want to validate input or somehow filter what can go in the textbox. string[] formats = { "ddd, d MMM yyyy HH:mm:ss zzz", "ddd, dd MMM yyyy HH:mm:ss zzz", "r", "R", "d", "D", "f", "F", "g", "G", "m", "M", "s", "t", "T", "U", "y", "Y" }; try { DateTime parsed = DateTime.ParseExact( textBox1.Text, formats, format, DateTimeStyles.AllowInnerWhite ); label1.Text = parsed.ToString( formats[0] ); } catch(Exception ex) { System.Console.WriteLine( "Message: {0}\nException: {1}", ex.Message, ex.ToString() ); } Link to comment Share on other sites More sharing options...
Question
+chorpeac MVC
Ok what is wrong with this...
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US", true); string dateStr = "Thu, 6 May 2004 18:08:13 -0400"; DateTime.ParseExact(dateStr,"ddd, dd MMM yyyy HH':'mm':'ss zzz",ci);Error: String was not recognized as a valid DateTime
Link to comment
Share on other sites
15 answers to this question
Recommended Posts