CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2003
    Posts
    815

    number of days in each month

    Hello,

    I was wondering if there is a function/class that returns the number of days in a month according to month & year

    I mean, for example it should retrun for February 28/29 according to the year

    Or should I write one of my own?

    Thanks
    Avi

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: number of days in each month

    Here's one.

    Code:
    #include <time.h>
    
    int getDaysOfMonthYear(int month, int year)
    {
    	tm currentMonth = {0, 0, 0, 1 /* first day of the month */, month-1 /* Zero-base */, year-1900 /* Time starts at 1900 */, 0, 0, 0};
    	tm nextMonth = {0, 0, 0, 1, month, year-1900, 0, 0, 0};
    	time_t diff = mktime(&nextMonth) - mktime(&currentMonth);
    
    	return diff/(60*60*24);
    }
    Last edited by Kheun; December 7th, 2004 at 03:38 AM.

  3. #3
    Join Date
    Sep 2004
    Posts
    519

    Re: number of days in each month

    And another using boost:ate_time:

    Code:
    #include <cstddef>
    #include <cstdlib>
    
    #include "boost/date_time/gregorian/gregorian.hpp"
    #include <iostream>
    
    int main(int, char*)
    {
        // Prints last day of feb 2004
        std::cout 
            << boost::gregorian::gregorian_calendar::end_of_month_day(2004, 2)
            << std::endl;
    
        return EXIT_SUCCESS;
    }
    See www.boost.org (seems like the site is down right now) for more information.


    Hope this helps

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured