Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple date format support #603

Closed
mguinness opened this issue Nov 8, 2016 · 5 comments
Closed

Multiple date format support #603

mguinness opened this issue Nov 8, 2016 · 5 comments
Labels

Comments

@mguinness
Copy link

I need to allow for more than a single date format for parsing dates when reading files. Dates can be in either yyyy-MM-dd or yyyyMMdd formats.

My initial attempt was to use TypeConverterOption and set multiple options, which didn't work (I assume that only the last format is used).

.TypeConverterOption("yyyy-MM-dd").TypeConverterOption("yyyyMMdd")

The DateTime class has a ParseExact overload which accepts an array of format strings, is there a way to incorporate that into the default DateTimeConverter class?

@JoshClose
Copy link
Owner

Yes! It should be fairly easy to do I think.

@kibbled
Copy link

kibbled commented Jan 22, 2017

Since TypeConverterOption is used for output and input how would it know which one to use when outputting CSV files? It would seem to implement this properly it would require some refactoring or a custom converter.

@jamesbascle
Copy link
Contributor

jamesbascle commented Jan 22, 2017 via email

@MichaelMcKechnie
Copy link

MichaelMcKechnie commented Feb 20, 2017

fyi in the meantime I have found it handy to use a simple extension method for this...

public static DateTime? DateConverter(this ICsvReaderRow row, string fieldName, string[] dateFormats)
{
    if (!String.IsNullOrEmpty(row.GetField(fieldName)))
    {
        return DateTime.ParseExact(row.GetField(fieldName), dateFormats, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
    }
    return null;
}

use it in a class map like this...

Map(p => p.RemovalDate).ConvertUsing(row => row.DateConverter("Removal_date", CsvClassMapExtension.DateFormats));

@JoshClose
Copy link
Owner

Just committed this. e8d56c7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants