• 0

C# Date/Time Convert


Question

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

  • 0

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

  • 0

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

  • 0

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

  • 0

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

  • 0

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

  • 0

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

  • 0

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

  • 0

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

  • 0

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

  • 0

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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.