问与答:我如何弄清楚这个月的最后一天是什么?

时间:2013-03-26 06:36:07

标签: c++ gregorian-calendar monthcalendar date-arithmetic leap-year

我正在尝试编写自己的时区转换器,我需要一种方法来确定本月的最后一天。通过一些研究,我发现了寻找闰年的公式。

这是一个小小的贡献,但也许我会在20分钟内拯救别人,让我弄明白并应用它。

此代码接受签名的短月,索引为0(0表示1月),并且int年也被索引为0(2012年是2012年)。

它返回1个索引日(27日是27日,但在SYSTEMTIME结构中,你通常需要0索引 - 只是抬头)。

4 个答案:

答案 0 :(得分:4)

short _get_max_day(short month, int year) {
    if(month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11)
        return 31;
    else if(month == 3 || month == 5 || month == 8 || month == 10)
        return 30;
    else {
        if(year % 4 == 0) {
            if(year % 100 == 0) {
                if(year % 400 == 0)
                    return 29;
                return 28;
            }
            return 29;
        }
        return 28;
    }
}

答案 1 :(得分:3)

怎么样?
#include <time.h>
#include <iostream>

int LastDay (int iMonth, int iYear)
{
    struct tm when;
    time_t lastday;

    // Set up current month
    when.tm_hour = 0;
    when.tm_min = 0;
    when.tm_sec = 0;
    when.tm_mday = 1;

    // Next month 0=Jan
    if (iMonth == 12)
    {
        when.tm_mon = 0;
        when.tm_year = iYear - 1900 + 1;
    }
    else
    {
        when.tm_mon = iMonth;
        when.tm_year = iYear - 1900;
    }
    // Get the first day of the next month
    lastday = mktime (&when);

    // Subtract 1 day
    lastday -= 86400;

    // Convert back to date and time
    when = *localtime (&lastday);

    return when.tm_mday;
}

int _tmain(int argc, _TCHAR* argv[])
{
    for (int m = 1; m <= 12; m++)
        std::cout << "Last day of " << m << " is " << LastDay (m, 2002) << std::endl;

    return 0;
}

打印出来(2002年)......

Last day of 1 is 31
Last day of 2 is 28
Last day of 3 is 31
Last day of 4 is 30
Last day of 5 is 31
Last day of 6 is 30
Last day of 7 is 31
Last day of 8 is 31
Last day of 9 is 30
Last day of 10 is 31
Last day of 11 is 30
Last day of 12 is 31

答案 2 :(得分:0)

我使用一个简单的函数,它返回(标准)COleDateTime的整个日期。它可能不是那么快的其他选择,但它非常有效,适用于闰年和非常简单的证据。

这是我正在使用的代码:

COleDateTime get_last_day_of_month(UINT month, UINT year)    
{
if(month == 2)
    {                                               // if month is feb, take last day of March and then go back one day
        COleDateTime    date(year, 3, 1, 0, 0, 0);  // 1 March for Year
        date -= 1;                                  // go back one day (the standard class will take leap years into account)
        return date;
    }
    else if(month == 4 || month == 6 || month == 9 || month == 11) return COleDateTime(year, month, 30, 0, 0, 0);
    else return COleDateTime(year, month, 31, 0, 0, 0);
}

答案 3 :(得分:-1)

Word Year, Month, Day;
TDateTime datum_tdatetime = Date();

// first day of actual month
datum_tdatetime.DecodeDate(&year, &month, &day);
day = 1;
datum_tdatetime = EncodeDate(year, month, day);
// last day of previous month
datum_tdatetime -= 1;
// first day of previous month
datum_tdatetime.DecodeDate(&year, &month, &day);
day = 1;
datum_tdatetime = EncodeDate(year, month, day);