user3784669
user3784669

Reputation: 1

How I can convert data format in filehelpers c#

I'm trying read this date 03/01/2020 00:17:41 from a .csv file, but when I try to convert to date with

[FieldConverter(ConverterKind.Date, "dd/MM/yyyy HH:mm:ss")]
public DateTime Field01_Fecha;

The log show me this error:

FileHelpers.ConvertException: Error Converting '03/01/2020 00:17:41' to type: 'DateTime'. There are more chars in the Input String than in the Format string: 'ddMMyyyy'

I tried with all date formats and none work for me.

Upvotes: 0

Views: 297

Answers (1)

sgmoore
sgmoore

Reputation: 16067

This should work. The error message is the sort of message you would get without the proper FieldConverter.

The following is a simple test which works for me.

void Main()
{
    string sampleData = @"03/01/2020 00:17:41";

    var engine = new FileHelperEngine<Test>();
    var test = engine.ReadString(sampleData);

    Console.WriteLine(test[0].fldDateTime);  
}

[DelimitedRecord(",")]
private class Test
{
    [FieldConverter(ConverterKind.Date, "dd/MM/yyyy HH:mm:ss")]
    public DateTime fldDateTime;

}

Upvotes: 1

Related Questions