C# 调用 ToString("YYYY-mm-dd") 导致日期格式错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16106394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling ToString("YYYY-mm-dd") results in wrong date format
提问by PiousVenom
I've got a constructor which takes a DateTimeobject:
我有一个构造函数,它接受一个DateTime对象:
public Report(DateTime date, string start = "0", string end = "0")
{
Logger.Info("Creating a new Report...");
StartTime = start;
EndTime = end;
Date = date.ToString("YYYY-mm-dd");
SetStartEndTimes();
Logger.Info("Report Created");
}
Now, this was working fine just 3 days ago. However, I come back today, after a break, and this is the results I'm seeing:
现在,这在 3 天前还可以正常工作。但是,休息后我今天回来了,这是我看到的结果:


As you can see, the date being passed in is right. However, after the format, it is not. Again, this worked before my break. I come back, and I get this. Am I missing something? Why would it format so incorrectly after working since the beginning?
如您所见,传入的日期是正确的。但是,格式化后,它不是。同样,这在我休息前奏效了。我回来了,我明白了。我错过了什么吗?为什么从一开始工作后格式会如此错误?
EDIT
编辑
Thanks guys. The messed up part is looking through the source control at previous versions, this worked. Or maybe I imagined it working. I don't know. But it's been this way for about 3 months.
谢谢你们。混乱的部分是查看以前版本的源代码管理,这是有效的。或者也许我想象它工作。我不知道。但这种情况已经持续了大约 3 个月。
采纳答案by Tim Schmelter
Year must be lowercase and month uppercase:
年份必须小写,月份大写:
Date = date.ToString("yyyy-MM-dd"); // btw, lowercase mm means minutes
回答by DGibbs
This:
这个:
Date = date.ToString("YYYY-mm-dd");
Should be this:
应该是这样的:
Date = date.ToString("yyyy-MM-dd");
Lowercase mmwill give you minutes.
小写mm会给你几分钟。

