BillyDvd
BillyDvd

Reputation: 176

DateTime.Parse always throws exception in a specific culture

I have some old log files I have to parse - apparently the date time was saved like: 18/12/2012 11:09:39 p.m. - All my attempts to parse these have failed. I am completely lost on this - Any help or direction would be great!

 CultureInfo cultureInfo = new CultureInfo( "es-MX" , true );
        string date = "18/12/2012 11:09:39 p.m.";

        DateTime dt = new DateTime( 2012 , 12 , 18 , 11 , 9 , 39 ).AddHours( 12 );

        this.richTextBox1.Text += date + Environment.NewLine;
        this.richTextBox1.Text += dt.ToString( cultureInfo ) + Environment.NewLine;
        this.richTextBox1.Text += dt.ToString() + Environment.NewLine;

        foreach ( var item in richTextBox1.Lines )
        {
            try
            {
               DateTime d=  DateTime.Parse( item );
               this.richTextBox1.Text += d.ToString() + Environment.NewLine ;

            }
            catch ( Exception ee)
            {
                this.richTextBox1.Text += ee.Message + Environment.NewLine ;

            }
        }

Upvotes: 8

Views: 1483

Answers (4)

BillyDvd
BillyDvd

Reputation: 176

Some dates are correct in the log file(s) some have the odd formatting that end in p. m. or p.m.. All methods above seem to fail - and yes I tried them all :( This was my hack/fix for the problem:

     CultureInfo cultureInfo = new CultureInfo( "es-MX" , true );
     Date = DateTime.Parse( date.Replace( "p. m." , "PM" ).Replace( "p.m." , "PM" ).Replace( "." , "" ).ToUpper() , cultureInfo );

Upvotes: 3

Nicholas Carey
Nicholas Carey

Reputation: 74187

The problem is that you're not specifying the culture to use in your call to Parse(). Your invocation uses the current thread's CurrentCulture property:

DateTime d=  DateTime.Parse( item );

The magic incantation you need is something like:

DateTime instance = DateTime.Parse( text , CultureInfo.GetCultureInfo("es-MX") ) ;

Your other alternative is to change the current thread's culture:

CultureInfo mexico = CultureInfo.GetCultureInfo( "es-MX" );
Thread.CurrentThread.CurrentCulture = mexico;

before you call DateTime.Parse(). Do that at startup and you should be good to go (so long as working in Mexican spanish is good for your purposes. Note that changing the current culture won't change how things are displayed: that's the responsibility of the thread's CurrentUICulture property.

Upvotes: 0

Dave
Dave

Reputation: 518

Try using your cultureInfo variable as the second parameter of Parse. This will use the culture as the format provider.

DateTime d =  DateTime.Parse( item, cultureInfo );

Upvotes: 0

Nick Vaccaro
Nick Vaccaro

Reputation: 5504

Try using DateTime.TryParseExact(). Here's an example that I ran in LINQPad.

void Main()
{
    System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo( "es-MX" , true );
    string date = "18/12/2012 11:09:39 p.m.";

    DateTime dt = new DateTime( 2012 , 12 , 18 , 11 , 9 , 39 ).AddHours( 12 );

    DateTime d;
    string[] styles = {"dd/MM/yyyy hh:mm:ss tt"}; // This doesn't have to be an array - could be string
    DateTime.TryParseExact(date, styles, cultureInfo, System.Globalization.DateTimeStyles.None, out d);

    d.Dump();
}

Upvotes: 2

Related Questions