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

C#.NetDatetime

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

Slash is a date delimiter, so that will use the current culture date delimiter.

If you want to hard-code it to always use slash, you can do something like this:

DateTime.ToString("dd'/'MM'/'yyyy")

Solution 2 - C#

Pass CultureInfo.InvariantCulture as the second parameter of DateTime, it will return the string as what you want, even a very special format:

DateTime.Now.ToString("dd|MM|yyyy", CultureInfo.InvariantCulture)

will return: 28|02|2014

Solution 3 - C#

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"

Solution 4 - C#

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

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

Solution 5 - C#

Dumb question/answer perhaps, but have you tried dd/MM/yyyy? Note the capitalization.

mm is for minutes with a leading zero. So I doubt that's what you want.

This may be helpful: http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDiskdriveView Question on Stackoverflow
Solution 1 - C#Bojan BjelicView Answer on Stackoverflow
Solution 2 - C#Liu PengView Answer on Stackoverflow
Solution 3 - C#Mikael EngverView Answer on Stackoverflow
Solution 4 - C#user10304366View Answer on Stackoverflow
Solution 5 - C#KonView Answer on Stackoverflow