Datetime.ToString() C# not working as expected

后端 未结 1 1151
清酒与你
清酒与你 2021-01-17 22:57

From msdn it seems like I can create my own format with Datetime.ToString() method by using M, m, d, y etc. But when I tried one it didn\'t worked

相关标签:
1条回答
  • 2021-01-17 23:32

    Looks like your DateSeparator of your CurrentCulture is - and that's why / character replace itself to it.

    "/" custom format specifier has a special meaning as replace me with current culture or supplied culture date separator.

    You have a few options, you either escape it with single quotes (or \/ in a verbatim string literal) or use a culture that has / as a DateSeparator like InvariantCulture.

    string s = DateTime.Now.ToString("M'/'d'/'yyyy");
    string s = DateTime.Now.ToString(@"M\/d\/yyyy");
    string s = DateTime.Now.ToString("M/d/yyyy", CultureInfo.InvariantCulture);
    
    0 讨论(0)
提交回复
热议问题