如果日期字符串匹配通用时间格式,FileHelpers 将仅解析日期时间
FileHelpers will only parse datetime if date string matches universal time format
我的日期时间字段仅在格式化程序设置为通用时间格式且日期时间已采用该格式时才会解析。这是预期的行为,还是它应该能够采用任何正确的日期字符串并将其格式化为不同的格式?如果是,如何解决?
如果日期输入已经是通用格式,此方法有效:
[FieldTrim(TrimMode.Both)]
[FieldConverter(ConverterKind.Date, "yyyy-MM-dd hh:mm")]
public DateTime Transaction_date; //where Transaction_date CSV value = "2019-01-02 6:00"
这些不起作用:
[FieldTrim(TrimMode.Both)]
[FieldConverter(ConverterKind.Date, "yyyy-MM-dd hh:mm")]
public DateTime Transaction_date; //where Transaction_date = "01-02-2019 6:00"
这是上述代码的例外情况:
"Message": "An error has occurred.",
"ExceptionMessage": "Error Converting '01-02-2009 06:00' to type: 'DateTime'. Using the format: 'yyyy-MM-dd hh:mm'",
"ExceptionType": "FileHelpers.ConvertException",
"StackTrace": " at FileHelpers.ConvertHelpers.DateTimeConverter.StringToField(String from)\r\n at FileHelpers.FieldBase.AssignFromString(ExtractedInfo fieldString, LineInfo line)\r\n at FileHelpers.FieldBase.ExtractFieldValue(LineInfo line)\r\n at FileHelpers.RecordOperations.StringToRecord(Object record, LineInfo line, Object[] values)\r\n at FileHelpers.FileHelperEngine1.ReadStreamAsList(TextReader reader, Int32 maxRecords, DataTable dt)\r\n at FileHelpers.FileHelperEngine
1.ReadStream(TextReader reader, Int32 maxRecords)\r\n at FileHelpers.FileHelperEngine`1.ReadString(String source, Int32 maxRecords)...
[FieldTrim(TrimMode.Both)]
[FieldConverter(ConverterKind.Date, "MM-dd-yyyy hh:mm")]
public DateTime Transaction_date; //where Transaction_date CSV value = "2019-01-02 6:00" Note that formatter is not universal date format
这是上述代码段的例外情况:
"Message": "An error has occurred.",
"ExceptionMessage": "Error Converting '2009-01-02 06:00' to type: 'DateTime'. Using the format: 'MM-dd-yyyy hh:mm'",
"ExceptionType": "FileHelpers.ConvertException",
"StackTrace": " at FileHelpers.ConvertHelpers.DateTimeConverter.StringToField(String from)\r\n at FileHelpers.FieldBase.AssignFromString(ExtractedInfo fieldString, LineInfo line)\r\n at FileHelpers.FieldBase.ExtractFieldValue(LineInfo line)\r\n at FileHelpers.RecordOperations.StringToRecord(Object record, LineInfo line, Object[] values)\r\n at FileHelpers.FileHelperEngine1.ReadStreamAsList(TextReader reader, Int32 maxRecords, DataTable dt)\r\n at FileHelpers.FileHelperEngine
1.ReadStream(TextReader reader, Int32 maxRecords)\r\n at FileHelpers.FileHelperEngine`1.ReadString(String source, Int32 maxRecords)...
根据 these docs,您可以使用 ConverterKind.DateMultiFormat
允许最多三种格式。
[FieldConverter(ConverterKind.DateMultiFormat, "yyyy-MM-dd HH:mm", "MM-dd-yyyy HH:mm")]
请注意,您的代码中有 hh
。这是 12 小时制的小时数,范围从 01 到 12。如果不为 am/pm 子午线指标指定 tt
,它不是很有用。您可能指的是 HH
,它表示 24 小时制中的小时数,范围从 00
到 23
。格式化标记区分大小写。
最终,您指定的格式被传递给DateTime.TryParseExact
,在the source code here. Thus, any of the .NET date and time format strings中可以使用。
关于您的问题:
... should it be able to take any proper date string ...
没有。它只会接受您提供的格式。
请记住,当文化不明确时,日期字符串可能会模棱两可。例如,01/02/2019
在美国是 1 月 2 日,但在世界其他大部分地区将被解释为 2 月 1 日。
我的日期时间字段仅在格式化程序设置为通用时间格式且日期时间已采用该格式时才会解析。这是预期的行为,还是它应该能够采用任何正确的日期字符串并将其格式化为不同的格式?如果是,如何解决?
如果日期输入已经是通用格式,此方法有效:
[FieldTrim(TrimMode.Both)]
[FieldConverter(ConverterKind.Date, "yyyy-MM-dd hh:mm")]
public DateTime Transaction_date; //where Transaction_date CSV value = "2019-01-02 6:00"
这些不起作用:
[FieldTrim(TrimMode.Both)]
[FieldConverter(ConverterKind.Date, "yyyy-MM-dd hh:mm")]
public DateTime Transaction_date; //where Transaction_date = "01-02-2019 6:00"
这是上述代码的例外情况:
"Message": "An error has occurred.", "ExceptionMessage": "Error Converting '01-02-2009 06:00' to type: 'DateTime'. Using the format: 'yyyy-MM-dd hh:mm'", "ExceptionType": "FileHelpers.ConvertException", "StackTrace": " at FileHelpers.ConvertHelpers.DateTimeConverter.StringToField(String from)\r\n at FileHelpers.FieldBase.AssignFromString(ExtractedInfo fieldString, LineInfo line)\r\n at FileHelpers.FieldBase.ExtractFieldValue(LineInfo line)\r\n at FileHelpers.RecordOperations.StringToRecord(Object record, LineInfo line, Object[] values)\r\n at FileHelpers.FileHelperEngine
1.ReadStreamAsList(TextReader reader, Int32 maxRecords, DataTable dt)\r\n at FileHelpers.FileHelperEngine
1.ReadStream(TextReader reader, Int32 maxRecords)\r\n at FileHelpers.FileHelperEngine`1.ReadString(String source, Int32 maxRecords)...
[FieldTrim(TrimMode.Both)]
[FieldConverter(ConverterKind.Date, "MM-dd-yyyy hh:mm")]
public DateTime Transaction_date; //where Transaction_date CSV value = "2019-01-02 6:00" Note that formatter is not universal date format
这是上述代码段的例外情况:
"Message": "An error has occurred.", "ExceptionMessage": "Error Converting '2009-01-02 06:00' to type: 'DateTime'. Using the format: 'MM-dd-yyyy hh:mm'", "ExceptionType": "FileHelpers.ConvertException", "StackTrace": " at FileHelpers.ConvertHelpers.DateTimeConverter.StringToField(String from)\r\n at FileHelpers.FieldBase.AssignFromString(ExtractedInfo fieldString, LineInfo line)\r\n at FileHelpers.FieldBase.ExtractFieldValue(LineInfo line)\r\n at FileHelpers.RecordOperations.StringToRecord(Object record, LineInfo line, Object[] values)\r\n at FileHelpers.FileHelperEngine
1.ReadStreamAsList(TextReader reader, Int32 maxRecords, DataTable dt)\r\n at FileHelpers.FileHelperEngine
1.ReadStream(TextReader reader, Int32 maxRecords)\r\n at FileHelpers.FileHelperEngine`1.ReadString(String source, Int32 maxRecords)...
根据 these docs,您可以使用 ConverterKind.DateMultiFormat
允许最多三种格式。
[FieldConverter(ConverterKind.DateMultiFormat, "yyyy-MM-dd HH:mm", "MM-dd-yyyy HH:mm")]
请注意,您的代码中有 hh
。这是 12 小时制的小时数,范围从 01 到 12。如果不为 am/pm 子午线指标指定 tt
,它不是很有用。您可能指的是 HH
,它表示 24 小时制中的小时数,范围从 00
到 23
。格式化标记区分大小写。
最终,您指定的格式被传递给DateTime.TryParseExact
,在the source code here. Thus, any of the .NET date and time format strings中可以使用。
关于您的问题:
... should it be able to take any proper date string ...
没有。它只会接受您提供的格式。
请记住,当文化不明确时,日期字符串可能会模棱两可。例如,01/02/2019
在美国是 1 月 2 日,但在世界其他大部分地区将被解释为 2 月 1 日。