2

I'm trying to export excel using ClosedXML and I have problem in fixing datetime format. My desired cell format is "dd.MM.yyyy HH:mm:ss" but when the day of the month is 12 or less, it automatically switches the day and the month and results in "MM.dd.yyyy HH:mm:ss".

var date = Convert.ToDateTime(colValue);
value = date.ToString("dd.MM.yyyy HH:mm:ss");
worksheet.Cell(x,y).Style.NumberFormat.Format = "dd.MM.yyyy HH:mm:ss";
worksheet.Cell(x,y).Value = value;

When the day of the month is 13 or above, it results in correct format.

Iraj
  • 319
  • 3
  • 17
  • 3
    Try set the cell value to the date float value instead of string: `double value=date.ToOADate(); worksheet.Cell(x,y).Value = value;` – Magnetron Nov 11 '22 at 17:36
  • Magnetron is right - it is the solution. You have chosen a date format that is interpreted differently in different parts of the world - only changing cultures inside of your OS could help, but bad practise. You could format the date like ISO8601, but in my experiences Microsoft does not like to follow worldwide standards. – BitLauncher Nov 11 '22 at 22:44

0 Answers0