You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
.NET Framework 4.8, CSVHelper 34.0.1
I would like to set a format for all DateTime values (yyyy-MM-dd, no time info). The solutions suggested here and on SO work on CSVHelper on .NET Core, but fail as indicated in my sample code:
public static void Main()
{
var records = new List<Test>
{
new Test { Id = 1, Name = "Spagaña, Antonio", ReceivedDate = DateTime.Parse("3/22/1972"), OptDate = DateTime.Parse("4/2/22") },
new Test { Id = 2, Name = "two", ReceivedDate = DateTime.Parse("1/23/1998"), OptDate=new DateTime(1982, 3, 22) },
};
var fileBytes = Array.Empty<byte>();
using (var stream = new MemoryStream())
{
using (var writer = new StreamWriter(stream, Encoding.UTF8))
{
using (var csv = new CsvWriter(writer, new CsvConfiguration(CultureInfo.InvariantCulture)))
{
csv.Context.RegisterClassMap(new TestMap());
csv.WriteRecords(records);
}
}
fileBytes = stream.ToArray();
}
HttpContext current = HttpContext.Current;
current.Trace.IsEnabled = false;
current.Response.Clear();
current.Response.ContentType = "application/octet-stream"; // or application/force-download
current.Response.AddHeader("content-disposition", "attachment; filename=SelectedRecords.csv");
current.Response.BinaryWrite(fileBytes);
current.Response.End();
}
public class Test
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime ReceivedDate { get; set; }
public DateTime? OptDate { get; set; }
}
public class TestMap : ClassMap<Test>
{
public TestMap()
{
var csvContext = new CsvContext(new CsvConfiguration(CultureInfo.InvariantCulture));
// 'Works', correctly formatted as specified
// Id,Name,ReceivedDate,OptDate
// 1,"Spagaña, Antonio",1972=03=22,2022=04=02
// 2,two,1998=01=23,1982=03=22
//csvContext.TypeConverterOptionsCache.GetOptions<DateTime>().Formats = new[] { "yyyy=MM=dd" };
//csvContext.TypeConverterOptionsCache.GetOptions<DateTime?>().Formats = new[] { "yyyy=MM=dd" };
// Fails, returning original unformatted input values
// Id Name ReceivedDate OptDate
// 1 Spagaña, Antonio 3/22/1972 4/2/2022
// 2 two 1/23/1998 3/22/1982
csvContext.TypeConverterOptionsCache.GetOptions<DateTime>().Formats = new[] { "yyyy-MM-dd" };
csvContext.TypeConverterOptionsCache.GetOptions<DateTime?>().Formats = new[] { "yyyy-MM-dd" };
AutoMap(csvContext);
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
.NET Framework 4.8, CSVHelper 34.0.1
I would like to set a format for all DateTime values (yyyy-MM-dd, no time info). The solutions suggested here and on SO work on CSVHelper on .NET Core, but fail as indicated in my sample code:
Beta Was this translation helpful? Give feedback.
All reactions