CsvHelper C# datetime format dd/mm/yyyy

Viewed 3282

I am using CsvHelper library to parse CSV data to C# object. So far, I can parse all this class with these 3 columns.

public class Foo
{
    public string Id { get; set; }
    public decimal Amount { get; set; }
    public string CurrencyCode { get; set; }
}

But after I added the DateTime property, it breaks. Class after added new column.

public class Foo
{
    public string Id { get; set; }
    public decimal Amount { get; set; }
    public string CurrencyCode { get; set; }
    public DateTime TransactionDate { get; set; }
}

This is my configuration

var config = new CsvConfiguration(CultureInfo.InvariantCulture)
            {
                HasHeaderRecord = false,                            
            };
            using (var reader = new StreamReader(filePath))
            using (var csv = new CsvReader(reader, config))
            {
                csv.Context.RegisterClassMap<CsvMap>();                            
                var records = csv.GetRecords<Foo>();
                foreach (var record in records)
                {
                    Console.WriteLine(record.Id);
                }
            }

public sealed class CsvMap : CsvHelper.Configuration.ClassMap<Foo>
{
    public CsvMap()
    {
        string format = "yyyy-MM-dd hh:mm:ss";
        var msMY = CultureInfo.GetCultureInfo("ms-MY");

        Map(m => m.TransactionDate).TypeConverterOption.Format(format)
          .TypeConverterOption.CultureInfo(msMY);
    }
}

and this is my data

"Invoice0000001","1,000.00","USD","20/02/2019 12:33:16"
"Invoice0000002","300.00","USD","21/02/2019 02:04:59"

Note: The date format in the above date is DD/MM/YYYY HH:MM:SS

1 Answers

Your map is only mapping the date and you don't have an index specified, so it's it's going to use the first column (index 0).

If your format is DD/MM/YYYY HH:MM:SS then why are you specifying yyyy-MM-dd hh:mm:ss as the format?

Changing those 2 things will fix your issue.

void Main()
{
    var s = new StringBuilder();
    s.Append("\"Invoice0000001\",\"1,000.00\",\"USD\",\"20/02/2019 12:33:16\"\r\n");
    s.Append("\"Invoice0000002\",\"300.00\",\"USD\",\"21/02/2019 02:04:59\"\r\n");
    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
        HasHeaderRecord = false,
    };
    using (var reader = new StringReader(s.ToString()))
    using (var csv = new CsvReader(reader, config))
    {
        csv.Context.RegisterClassMap<CsvMap>();
        csv.GetRecords<Foo>().ToList().Dump();
    }
}

public class Foo
{
    public string Id { get; set; }
    public decimal Amount { get; set; }
    public string CurrencyCode { get; set; }
    public DateTime TransactionDate { get; set; }
}

public sealed class CsvMap : CsvHelper.Configuration.ClassMap<Foo>
{
    public CsvMap()
    {
        string format = "dd/MM/yyyy hh:mm:ss";
        var msMY = CultureInfo.GetCultureInfo("ms-MY");

        Map(m => m.TransactionDate).TypeConverterOption.Format(format)
          .TypeConverterOption.CultureInfo(msMY).Index(3);
    }
}

enter image description here

Also, Format is Format(params string[] formats) so you can pass multiple in if you have more than 1 format that could appear.

Related