173

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
  • do DateTime.ToString("dd/MM/yyyy");
    – jimplode
    Commented Jun 14, 2011 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
    Commented Jun 14, 2011 at 12:17
  • 7
    use MM rather than mm - mm represents minutes and not months. Commented Jun 14, 2011 at 12:30
  • 1
    possible duplicate of How to format a date with slashes in C#
    – Dzyann
    Commented Jul 29, 2015 at 13:44
  • The MSDN documentation for DateTime.ToString 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." Commented Nov 22, 2016 at 12:30

5 Answers 5

263

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")
7
  • 2
    @spender - in some cultures they are. Commented Jun 14, 2011 at 12:19
  • 6
    You could also use this format: dateTime.ToString(@"yyyy\/MM\/dd")
    – base2
    Commented Nov 26, 2012 at 15:22
  • 1
    MSDN reference: msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#dateSeparator
    – russau
    Commented Jun 18, 2013 at 16:50
  • @DanielA.White There are countries that have more lefties than right handers ? I mean when I think about a slash with a sword (right hander normal) it makes a / :)
    – Bitterblue
    Commented Mar 7, 2014 at 8:46
  • 2
    Escaping the format string using backslash does also work: DateTime.Now.ToString("dd\\/MM\\/yyyy");
    – TomB
    Commented Jan 30, 2015 at 8:42
77

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

2
  • 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
    Commented Jun 22, 2016 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
    Commented Jul 28, 2016 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"
2
  • 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 Commented Nov 13, 2017 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”; Commented Nov 15, 2017 at 8:09
0

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

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

Try this.

var DateFormat = "dd-MMM-yy"; var NumFormat = "###,###,##0.00"

 @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>
            }
1
  • please provide some context for the code
    – PawZaw
    Commented Dec 28, 2022 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.