Date separator issue

前端 未结 3 1672
小鲜肉
小鲜肉 2021-01-11 11:55

I have the following code

DateTime.Now.ToString(\"MM/dd/yyyy\")

It always gives me this output : \"04.13.2011\" instead of \"04/13/2011\".

相关标签:
3条回答
  • 2021-01-11 12:31

    You're almost certainly in a culture where that's the default date separator. If you want to force / you can quote it in the format string:

    string x = DateTime.Now.ToString("MM'/'dd'/'yyyy")
    
    0 讨论(0)
  • 2021-01-11 12:39

    Use following code:

    DateTime.Now.ToString("MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)
    

    This ensures that the underlying date and time values do not change when the data is read or written by users from different cultures.

    0 讨论(0)
  • 2021-01-11 12:42

    Try this

    DateTime.Now.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)
    
    0 讨论(0)
提交回复
热议问题