DateTime.Now.ToString(“MM/DD/YYYY”) contains incorrect month

怎甘沉沦 提交于 2019-12-02 13:05:35

Use this instead:

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

"MM" for month. "dd" for days. "yyyy" for year.

"MM/DD/YYYY" is wrong format:

Console.WriteLine(DateTime.Now.ToString("MM/DD/YYYY")) // prints "06/DD/YYYY"
DateTime.Now.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);

As already answered you need to have format specifier for day and year in small letters but as an add on you should also specify the culture variable to make sure that final output contains "/" between date parts. Without this culture parameter "/" could be replace by the date separator of the culture of system where code is running like "-".

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