Date separator issue

天大地大妈咪最大 提交于 2019-11-30 23:22:35

问题


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". May I know why I am getting this weird issue?


回答1:


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")



回答2:


Try this

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



回答3:


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.



来源:https://stackoverflow.com/questions/5641075/date-separator-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!