为什么YYYY-MM-DD HH&39;:'MM':'SS&Q;DATETIME格式显示不正确?

人气:639 发布:2022-10-16 标签: c# datetime timezone datetime-format

问题描述

 DateTime timeUtcWhenCommentPostingOccurred = getDateAndTimeOfCommentPostingInUtc();
 DateTime estTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtcWhenCommentPostingOccurred, estZone);


 estTime.ToString("YYYY-MM-DD HH':'MM':'SS");

以上指定的格式显示以下不正确的日期格式:

YYYY-11-DD 21:11:SS

为什么年、日和秒不能正确显示?请就如何解决上述问题提供建议。

推荐答案

因为表示年的是小写的y,而不是大写的Y。 Day也是如此,其小写d而不是大写D 其小写m不是大写M,大写M是 对于月份, 秒数为小写s,而不是大写S 还要删除格式中的单引号,因为您不想转义字符串文字

参见:Custom Date and Time Format Strings

因此您的格式应为:

estTime.ToString("yyyy-MM-dd HH:mm:ss");

348