简体   繁体   中英

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.

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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM