why does DateTime.ToString("dd/MM/yyyy") give me dd-MM-yyyy?

Viewed 127314

I want my datetime to be converted to a string that is in format "dd/MM/yyyy"

Whenever I convert it using DateTime.ToString("dd/MM/yyyy"), I get dd-MM-yyyy instead.

Is there some sort of culture info that I have to set?

5 Answers

Add CultureInfo.InvariantCulture as an argument:

using System.Globalization;

...

var dateTime = new DateTime(2016,8,16);
dateTime.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);

Will return:

"16/08/2016"

If you use MVC, tables, it works like this:

<td>@(((DateTime)detalle.fec).ToString("dd'/'MM'/'yyyy"))</td>
Related