1

I have an issue with parsing date values with CsvReader. In my test data the dates were in this format:

01/03/2021

Therefore, my CsvReader property is designed like this:

[Format("dd/MM/yyyy")]
[Name("Date")]
public DateTime Date { get => HistoryWeek.Week; set => HistoryWeek.Week = value; }

The above works OK. But then I asked a beta user to try my application and they hit an exception:

<?xml version="1.0" encoding="utf-8"?>
<LogEntries>
  <LogEntry Date="2022-01-16 22:15:13" Severity="Exception" Source="MSAToolsLibrary.Importer.Importer.ImportHistoryFromCLMExplorer" ThreadId="1">
    <Exception Type="CsvHelper.ReaderException" Source="CsvHelper.CsvReader+&lt;GetRecords&gt;d__87`1.MoveNext">
      <Message>An unexpected error occurred.
IReader state:
   ColumnCount: 0
   CurrentIndex: 0
   HeaderRecord:
["Date","Status","Weekly Bible Reading","Song1 #","Song1 Title","Song1 Scripture","Song2 #","Song2 Title","Song2 Scripture","Song3 #","Song3 Title","Song3 Scripture","Meeting?","CO Visit","Cancel Reason","# Classes","Chairman","PrayerOpen","PrayerClose","TreasuresTalk","TreasuresTalk_Theme","TreasuresDigging","BibleReading_A","BibleReading_Study_A","BibleReading_B","BibleReading_Study_B","BibleReading_C","BibleReading_Study_C","BibleReading_Source","Apply1_Description","Apply2_Description","Apply3_Description","Apply4_Description","Apply1_A","Apply1_Asst_A","Apply1_Study_A","Apply2_A","Apply2_Asst_A","Apply2_Study_A","Apply3_A","Apply3_Asst_A","Apply3_Study_A","Apply4_A","Apply4_Asst_A","Apply4_Study_A","Apply1_B","Apply1_Asst_B","Apply1_Study_B","Apply2_B","Apply2_Asst_B","Apply2_Study_B","Apply3_B","Apply3_Asst_B","Apply3_Study_B","Apply4_B","Apply4_Asst_B","Apply4_Study_B","Apply1_C","Apply1_Asst_C","Apply1_Study_C","Apply2_C","Apply2_Asst_C","Apply2_Study_C","Apply3_C","Apply3_Asst_C","Apply3_Study_C","Apply4_C","Apply4_Asst_C","Apply4_Study_C","LivingPart1","LivingPart1_Theme","LivingPart1_Length","LivingPart2","LivingPart2_Theme","LivingPart2_Length","CBS","CBS_Source","CBS_Read","Audience B","Audience C","AuxCounselor B","AuxCounselor C"]
IParser state:
   ByteCount: 0
   CharCount: 1483
   Row: 2
   RawRow: 2
   Count: 82
   RawRecord:
1/03/2021,Unscheduled,NUMBERS 7-8,4,“Jehovah Is My Shepherd”,Psalm 23,54,“This Is the Way”,"Isaiah 30:20, 21",127,The Sort of Person I Should Be,2 Peter 3:11,Y,N,,1,,,,,“Lessons From the Camp of Israel”,,,5,,,,,Nu 7:1-17,Mem. Inv.,RV,RV,RV,,,11,,,6,,,12,,,17,,,,,,,,,,,,,,,,,,,,,,,,,,Organizational Accomplishments,(5 min.),,Local Needs,(10 min.),,"rr chap. 5 ¶17-22, box 5A",,,,,

</Message>
      <Exception Type="System.FormatException" Source="System.DateTimeParse.ParseExactMultiple">
        <Message>String was not recognized as a valid DateTime.</Message>
        <StackTrace>   at System.DateTimeParse.ParseExactMultiple(String s, String[] formats, DateTimeFormatInfo dtfi, DateTimeStyles style)
   at CsvHelper.TypeConversion.DateTimeConverter.ConvertFromString(String text, IReaderRow row, MemberMapData memberMapData)
   at lambda_method(Closure )
   at CsvHelper.Expressions.RecordCreator.Create[T]()
   at CsvHelper.CsvReader.&lt;GetRecords&gt;d__87`1.MoveNext()</StackTrace>
      </Exception>
    </Exception>
  </LogEntry>
</LogEntries>

I asked for his data and notice that his dates are slightly different:

1/03/2021

So the format of the dates is different:

  • dd/MM/yyyy
  • d/MM/yyyy

How do I change my CsvReader property to cope with this and avoid the exception?


Update

I noticed this similar discussion on Multiple date format support #603 . I need to work out how to adopt the advice there. It seems to indicate that it is possible to cater for multiple date formats. Problem is, I am not using a class map.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • Ah!, I see in the linked GitHub push commit that the `Format` specifier for the property can accept a list of formats: `[Format("dd/MM/yyyy", "d/MM/yyyy")]`. – Andrew Truckle Jan 16 '22 at 14:07

0 Answers0