使用 TryParseExact 将整数值格式化为时间

发布于 12-17 12:25 字数 514 浏览 4 评论 0原文

我有一个整数,其值为年月日。例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烏雲後面有陽光2024-12-24 12:25:08

我相信如果您的输入字符串没有连字符,格式字符串应该是 "yyyyMMdd"

I believe the format string should be "yyyyMMdd" if your input string has no hyphens.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文