171

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?

shashwat
  • 7,851
  • 9
  • 57
  • 90
Diskdrive
  • 18,107
  • 27
  • 101
  • 167
  • do DateTime.ToString("dd/MM/yyyy"); – jimplode Jun 14 '11 at 12:15
  • 2
    I did that, it didn't work, the problem isn't the dates, it's the "-", I want it to be a "/" like I've specified – Diskdrive Jun 14 '11 at 12:17
  • 7
    use MM rather than mm - mm represents minutes and not months. – Lloyd Powell Jun 14 '11 at 12:30
  • 1
    possible duplicate of [How to format a date with slashes in C#](http://stackoverflow.com/questions/3684033/how-to-format-a-date-with-slashes-in-c-sharp) – Dzyann Jul 29 '15 at 13:44
  • The [MSDN documentation for DateTime.ToString](https://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx#Anchor_2) is hopelessly wrong: "*For example, the “MM/dd/yyyyHH:mm” format string displays the date and time string in a fixed format ... The format string uses “/” as a fixed date separator regardless of culture-specific settings.*" – Colonel Panic Nov 22 '16 at 12:30

6 Answers6

262

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")
shashwat
  • 7,851
  • 9
  • 57
  • 90
Bojan Bjelic
  • 3,522
  • 1
  • 17
  • 18
76

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

Liu Peng
  • 1,226
  • 2
  • 11
  • 13
  • 1
    This code is more clear, than in accepted answer (I mean added CultureInfo). It looks at least better than escaping slashes by apostrophes. – Sergey Jun 22 '16 at 15:09
  • 3
    Definitely easier to read than using escape characters. I'm a bit shocked that I didn't know the whole time that my date formats were going to get overwritten! – Savage Jul 28 '16 at 14:09
14

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"
Mikael Engver
  • 4,634
  • 4
  • 46
  • 53
  • if the developer uses tostring method with some letters like M m ss etc. will get wrong result with your solution for example Datetime.Now.ToString("yyyy/dd/mm/Month",CultureInfo.InvariantCulture) will not result 2017/01/02/Month it will result 2017/01/02/2onth – Okan SARICA Nov 13 '17 at 17:22
  • @OkanSARICA if you would want to suffix the date with ”/Month” you should do it after the ToString method instead. DateTime.Now.ToString(”yyyy/MM/dd”, CultureInfo.InvariantCulture) + ”/Month”; – Mikael Engver Nov 15 '17 at 08:09
1

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

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

Try this.

 @foreach (DataRow _Row in CashBook2.Rows)
            {
                <tr>
                    <td>@DateTime.Parse(_Row["Vou_Date"].ToString()).ToString(DateFormat)</td>
                    <td>@_Row["Vou_No"].ToString()</td>
                    <td>@_Row["Description"].ToString()</td>
                    <td>@decimal.Parse(_Row["DR"].ToString()).ToString(NumFormat)</td>
                    <td>@decimal.Parse(_Row["CR"].ToString()).ToString(NumFormat)</td>
                    <td>@Balance</td>
                </tr>
            }
-3

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

Kon
  • 27,113
  • 11
  • 60
  • 86