VS 2010升级后出现奇怪的FormatException

发布于 2024-09-08 21:03:27 字数 1128 浏览 10 评论 0原文

我升级到 VS 2010 后得到这个 FormatException。没什么特别的。 代码:

private void ManageDateEditControls()
{
    apoDateEdit.DateTime = DateTime.Parse(string.Format("01/{0}/{1}", DateTime.Now.Month-1, DateTime.Now.Year));
    eosDateEdit.DateTime = DateTime.Parse(string.Format("{0}/{1}/{2}", GetLastDayOfMonth(DateTime.Now.Month + 1),
        DateTime.Now.Month - 1, DateTime.Now.Year)); <-- FormatException occurs in this line.
}

private static int GetLastDayOfMonth(int month)
{
    // set return value to the last day of the month
    // for any date passed in to the method

    // create a datetime variable set to the passed in date
    DateTime dtTo = new DateTime(DateTime.Now.Year, month, 1);

    // overshoot the date by a month
    dtTo = dtTo.AddMonths(1);

    // remove all of the days in the next month
    // to get bumped down to the last day of the
    // previous month
    dtTo = dtTo.AddDays(-(dtTo.Day));

    // return the last day of the month
    return dtTo.Day;
}

假设您在 2010 年 6 月 31 日运行此命令,就会得到现在的结果。我认为这是一个有效的日期。 我已经测试了生成的日期,没问题...这个项目在 VS 2008 中工作时从未遇到过这个问题。

有什么想法吗?

I get this FormatException after i upgraded to VS 2010. Not anything really special.
Code:

private void ManageDateEditControls()
{
    apoDateEdit.DateTime = DateTime.Parse(string.Format("01/{0}/{1}", DateTime.Now.Month-1, DateTime.Now.Year));
    eosDateEdit.DateTime = DateTime.Parse(string.Format("{0}/{1}/{2}", GetLastDayOfMonth(DateTime.Now.Month + 1),
        DateTime.Now.Month - 1, DateTime.Now.Year)); <-- FormatException occurs in this line.
}

private static int GetLastDayOfMonth(int month)
{
    // set return value to the last day of the month
    // for any date passed in to the method

    // create a datetime variable set to the passed in date
    DateTime dtTo = new DateTime(DateTime.Now.Year, month, 1);

    // overshoot the date by a month
    dtTo = dtTo.AddMonths(1);

    // remove all of the days in the next month
    // to get bumped down to the last day of the
    // previous month
    dtTo = dtTo.AddDays(-(dtTo.Day));

    // return the last day of the month
    return dtTo.Day;
}

Lets say you get now if you run this 31/6/2010. I think its a valid date.
I have tested the date that is generated and its ok...this project never had this problem while was working in VS 2008.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

爱给你人给你 2024-09-15 21:03:27

您的 FormatException 是由于将 31/6/2010 作为参数传递给 DateTime.Parse() 引起的。 2010 年 6 月 31 日不是有效日期 - 六月只有 30 天。

如果您需要任何月份的最后一天,最好使用 DateTime.DaysInMonth() 方法。它以月份和年份作为参数,因此它可以处理闰年。

Your FormatException is caused by passing 31/6/2010 as an argument to DateTime.Parse(). 31/6/2010 is not a valid date - there are only 30 days in June.

If you need the last day in any month, you would be better off using the DateTime.DaysInMonth() method. It takes both the month and year as arguments so it can deal with leap years.

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