使用 TryParseExact 将整数值格式化为时间
我有一个整数,其值为年月日。例如 20110504。
我使用 TryPareseExact
将其格式化为 yyyy-MM-dd
但它不起作用。
这是我的函数
public DateTime DateDisplay(int date)
{
DateTime dateValue;
if (DateTime.TryParseExact(date.ToString(), "yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out dateValue))
return dateValue;
else
return DateTime.MinValue;
}
,它总是转到 else 并返回 DateTime.MinValue
。我希望日期看起来像 2011/05/04。你能帮我找出我的错误在哪里吗?
I have an integer that will have the value of a year month and day. For example 20110504.
I am using TryPareseExact
to format it into yyyy-MM-dd
but it is not working.
Here is my function
public DateTime DateDisplay(int date)
{
DateTime dateValue;
if (DateTime.TryParseExact(date.ToString(), "yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out dateValue))
return dateValue;
else
return DateTime.MinValue;
}
It always go to the else and returns DateTime.MinValue
. I want the date to be look like 2011/05/04. Would you be able to help me to identify where is my mistake?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

我相信如果您的输入字符串没有连字符,格式字符串应该是
"yyyyMMdd"
。I believe the format string should be
"yyyyMMdd"
if your input string has no hyphens.