Note that there are some explanatory texts on larger screens.

plurals
  1. PORobust DateTime parser library for .NET
    primarykey
    data
    text
    <p>I am writing an RSS and Mail reader app in C# (technically MonoTouch).</p> <p>I have run into the issue of parsing <code>DateTime</code>s. I see a lot of variance in how dates are presented in the wild and have begun writing a function like this:</p> <pre><code>static string[] DateTimeFormats = new string[] { "ddd, d MMM yyyy H:mm:ss \"GMT+00:00\"", "d MMM yyyy H:mm:ss \"EST\"", "yyyy-MM-dd\"T\"HH:mm:ss\"Z\"", "ddd MMM d HH:mm:ss \"+0000\" yyyy", }; public static DateTime ParseTime(string timeStr) { var r = DateTime.MinValue; var styles = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AllowWhiteSpaces; if (DateTime.TryParse(timeStr, CultureInfo.InvariantCulture, styles, out r)) { return r; } else { if (DateTime.TryParseExact(timeStr, DateTimeFormats, CultureInfo.InvariantCulture, styles, out r)) { return r; // BUGGY! Ignores time zone!! } } Console.WriteLine ("BAAAAAAAAAAAAD"); return DateTime.MinValue; } </code></pre> <p>This, well, makes me sick. Two points. (1) It's silly of me to think that I can actually collect a format list that will cover everything out there. (2) It's wrong! Notice that I'm treating an EST date time as UTC (since .NET seems oblivious to time zones).</p> <p>I am looking for an existing library (source only please) that is known to handle a bunch of these formats.</p> <p>Also, I would like to keep using UTC DateTimes throughout my code so whatever library is suggested should be able to produce DateTimes.</p> <p>Is there anything out there like this?</p> <p><strong>Update</strong> It would seem I was unclear in my request. I am looking for a library that knows a lot of these <strong>wild</strong> format strings. DateTime.Parse/DateTime.TryParse only know a couple formats, and they certainly don't understand timezones (you can see I am already using it). I need something more powerful than DateTime.Parse/DateTime.TryParse.</p> <p><strong>Update 2</strong> Everyone was dwelling on the fact that I was using Parse instead of TryParse. I have switched the code. <strong>But it's still wrong and incomplete.</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload