Costa.Gustavo
Costa.Gustavo

Reputation: 849

Error mapping Datetime type field to .xlsx file

I map a .xlsx file and am trying to read a column that the data corresponding to it must be of type DateTime. The problem is that this data comes in the form "dd-mm-yyyy", and therefore the extension function I use does not recognize it. I need to read this value and convert it to the "dd / MM / yyyy" format. I'm trying to do the following:

Map(i => i.TradeDate)
   .Format(i => DateTime.ParseExact(i.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture))
   .WithColumnName("Data de Negociação");

Thus, the program assigns the value 01/01/0001. Could someone indicate a method to read values ​​in "dd-MM-yyyy" and convert to "dd / MM / yyyy"?

Upvotes: 0

Views: 71

Answers (1)

user2316116
user2316116

Reputation: 6814

Use:

var input = "29-04-2020"; //dd-mm-yyyy
DateTime d = DateTime.ParseExact(input, "dd-MM-yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(d.ToString("MM/dd/yyyy"));

Demo: https://dotnetfiddle.net/uJLv3e

Upvotes: 2

Related Questions