I need to read a CSV file using CsvReader, however I have a custom date format (yyyyMMdd) which by default can't be converted to DateOnly
. How can I specify this custom date format? Is that may possible using some annotation?
This is the code I currently have, it fails with
FormatException: String '20230817' was not recognized as a valid DateOnly.
class Foo
{
public string Title { get; set; } = null!;
public DateOnly Date { get; set; }
}
var sb = new StringBuilder();
sb.Append(@"Title,Date");
sb.AppendLine();
sb.Append(@"hello world,20230817");
sb.AppendLine();
using var stringReader = new StringReader(sb.ToString());
using var csvReader = new CsvReader(stringReader, CultureInfo.InvariantCulture);
int lineIndex = 0;
while(csvReader.Read())
{
if(lineIndex == 0)
{
csvReader.ReadHeader();
} else
{
var foo = csvReader.GetRecord<Foo>();
}
lineIndex++;
}