I have a CSV file that contains (among other fields) (nullable) DateTime values that are formatted like "2021-11-20 14:16:52.255421" I want to get rid of the milliseconds or whatever that is.
I have tried like this, but somehow the date format remains the same:
var csvConfiguration = new CsvConfiguration(CultureInfo.InvariantCulture);
csvConfiguration.HasHeaderRecord = true;
csvConfiguration.Delimiter = ";";
csvConfiguration.TrimOptions = TrimOptions.Trim;
Record[] records;
using (var reader = new StreamReader(CsvPath, Encoding.UTF8))
using (var csv = new CsvReader(reader, csvConfiguration))
{
records = csv.GetRecords<Record>().ToArray();
}
using (var writer = new StreamWriter($@"reformatted.csv"))
using (var csv = new CsvWriter(writer, CultureInfo.CurrentCulture))
{
//I have also tried with these options, but it doesn't help either
//var options = new TypeConverterOptions { Formats = new[] { "yyyy-MM-dd HH:mm:ss" } };
//csv.Context.TypeConverterOptionsCache.AddOptions<DateTime>(options);
csv.WriteRecords(records.Where(x => x.Memo != null));
}
// "mbox_id";"email_address";"created";"updated";"created_by";"memo";
class Record
{
[Name("mbox_id")]
public string MailboxId { get; set; }
[Name("email_address")]
public string EmailAddress { get; set;}
[Name("created")]
public DateTime? Created { get; set; }
[Name("updated")]
public DateTime? Updated { get; set; }
[Name("created_by")]
public string CreatedBy { get; set;}
[Name("memo")]
public string Memo { get; set; }
}
What am I doing wrong? I really don't understand how the CsvWriter would even know about the input format...