locked
The last day of the month RRS feed

  • Question

  • How do I get the last date of the month. For example Febraury 2009. Different last day for Febraury from year to year. Thanks.
    Friday, May 22, 2009 5:44 AM

Answers

  • check this,

    int

    day= DateTime.DaysInMonth(2008, 2);

    • Marked as answer by Kah Wooi Friday, May 22, 2009 6:20 AM
    Friday, May 22, 2009 6:04 AM

All replies

  • Hope this isn't homework...

    static int GetLastDayOfMonth(int month, int year)
    {
        if (month == 12)
        {
            month = 1;
            year++;
        }
        else
        {
            month++;
        }
        DateTime auxDate = new DateTime(year, month, 1);
        return auxDate.Subtract(new TimeSpan(1, 0, 0, 0)).Day;
    }

    Also, leap years are those that are divisible by 4 while not being divisible by 100 except if they are by 400. If you wish to use this rule instead, you'll have to code it ;)

    /* No comments */
    Friday, May 22, 2009 5:51 AM
  • Something like:

    Calendar c = Thread.CurrentThread.CurrentCulture.Calendar;
    Console.WriteLine (c.GetDaysInMonth ( 2009, 2 ) );

    HTH
    Tom Shelton
    Friday, May 22, 2009 5:52 AM
  • check this,

    int

    day= DateTime.DaysInMonth(2008, 2);

    • Marked as answer by Kah Wooi Friday, May 22, 2009 6:20 AM
    Friday, May 22, 2009 6:04 AM