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

前端 未结 5 1280
孤街浪徒
孤街浪徒 2020-11-29 19:52

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

相关标签:
5条回答
  • 2020-11-29 20:24

    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")
    
    0 讨论(0)
  • 2020-11-29 20:25

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

    <td>@(((DateTime)detalle.fec).ToString("dd'/'MM'/'yyyy"))</td>
    
    0 讨论(0)
  • 2020-11-29 20:27

    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

    0 讨论(0)
  • 2020-11-29 20:39

    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"
    
    0 讨论(0)
  • 2020-11-29 20:49

    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

    0 讨论(0)
提交回复
热议问题