tags:

views:

226

answers:

7

Does anyone know of a library (paid or not) out there that is able to handle more common datetime formats than DateTime.Parse uses? Something that can handle multiple languages and abbreviations for days/months would be really nice.

For example: "Sept 1, 2009" does not work with Datetime.Parse.

+1  A: 

To handle multiple languages, you can call DateTime.Parse with different cultures.

DateTime.Parse will handle abbreviations out of the box.

If you need to handle non-standard combinations, call DateTime.ParseExact.

SLaks
Yeah, I know. I would like a stronger implementation, though, for strings that may not follow datetime abbreviation standards for example.
Mike Gates
+1  A: 

I can't answer your question. Consider DateTime.ParseExact if you don't get an answer you like.

lance
Apparently, you can answer the question. :-)
SLaks
I know about DateTime.ParseExact, but I'm looking for an implementation of DateTime.Parse which can figure out more datetime formats than the standard one does.
Mike Gates
+2  A: 

I also have to point out that the DateTime.TryParse and DateTIme.TryParseExact methods exist. I think it's incredibly gross that you need to worry about exceptions from attempting to convert a string to a DateTime or <insert object type here>.

Chris Marisic
+4  A: 

I've used code like the following in the past (where the expectedDateTimeFormats are strings that follow the rules for Custom Date and Time Format Strings):

// Or use custom DateTimeFormatInfo objects
string[] expectedDateTimeFormats = new string[] {
    "customFormat1",
    "customFormat2",
    "customFormatN",
};

// You could offer several overloads here, to accept other DateTimeStyles,
// InvariantCulture, CurrentUICulture, etc. - perhaps even a collection of
// CultureInfo objects to try
public DateTime TryParseDateString(string dateString, CultureInfo culture) {
    try {
        // first, try to parse given the specified culture's formats
        return DateTime.Parse(dateString, culture);
    }
    catch (FormatException) {
        // if that fails, try your custom formats
        return DateTime.ParseExact(dateString, 
                                   expectedDateTimeFormats, 
                                   culture,
                                   DateTimeStyles.None);
    }
}

If you're dealing with non-standard abbreviations (like "Sept 1, 2009" as you mentioned in a comment), you may have to create a custom CultureInfo or DateTimeFormatInfo and define them yourself.

I don't know of a really good list of 'standard' custom formats (and/or related DateTimeFormatInfo definitions) - if anyone can find one they certainly deserve to be the accepted answer. I no longer have access to one my old team used (and wouldn't have had permission to share it anyway :( ).

Jeff Sternal
This is going somewhat in the right direction of what I want. Maybe a list of these custom formats somewhere? In addition to this is the problem of different abbreviations for month names, though.
Mike Gates
I don't know of a good list out there (though chances are good a google search will turn one up), but if you supply the culture, that should take care of the month and day abbreviations.
Jeff Sternal
DateTime.Parse does not understand "Sept 1, 2009". This is what I am talking about here.
Mike Gates
The whole point of this design was to allow custom formats in exactly this fashion. No new implementation required.
Dustman
A: 

Dont know if it has any "better" parsers, but you could check out The Skeet's OS project, Noda Time

Muad'Dib
A: 

To parse dates with non-standard abbreviations, you'll need to create your own culture.

For example: (Tested)

var culture = new CultureInfo("en-US");
culture.DateTimeFormat.AbbreviatedMonthNames = new string[] { 
    "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec", "" 
};

DateTime.Parse("Sept 1, 2009", culture);
SLaks
Hmmm...that might be a way to go. Now if I could just find a predefined set of custom cultures that deal with all kinds of crazy abbreviations in different cultures.
Mike Gates