Calling ToString(“YYYY-mm-dd”) results in wrong date format

Deadly 提交于 2019-12-10 01:12:00

问题


I've got a constructor which takes a DateTime object:

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:

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.


回答1:


Year must be lowercase and month uppercase:

Date = date.ToString("yyyy-MM-dd");  // btw, lowercase mm means minutes

Custom Date and Time Format Strings




回答2:


This:

Date = date.ToString("YYYY-mm-dd");

Should be this:

Date = date.ToString("yyyy-MM-dd");

Lowercase mm will give you minutes.



来源:https://stackoverflow.com/questions/16106394/calling-tostringyyyy-mm-dd-results-in-wrong-date-format

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