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日を実行すると、今すぐ取得できるとしましょう。有効な日付だと思います。生成された日付とそのOKをテストしました...このプロジェクトでは、VS2008で作業しているときにこの問題が発生することはありませんでした。

何か案は?

4

1 に答える 1

1

あなたはに引数としてFormatException渡すことによって引き起こされます。2010年6月31日は有効な日付ではありません-6月は30日しかありません。31/6/2010DateTime.Parse()

いずれかの月の最終日が必要な場合は、このDateTime.DaysInMonth()方法を使用することをお勧めします。うるう年に対応できるように、引数として月と年の両方が必要です。

于 2010-07-12T00:08:43.897 に答える