问答:我如何知道本月的最后一天是什么?

Q&A: How do I figure out what the last day of the month is?

本文关键字:最后一天 是什么 何知道 问答      更新时间:2023-10-16

我试图编写一个滚动您自己的时区转换器,我需要一种方法来确定一个月的最后一天是什么。经过一番研究,我发现了闰年的计算公式。

这是一个小小的贡献,但也许我会把我花在计算和应用它上的20分钟时间留给别人

此代码接受索引为0的带符号短月(0是1月(和索引为0(2012是2012(的整数年。

它返回一个索引为1的日期(第27天是第27天,但在SYSTEMTIME结构等中,您通常需要索引为0的日期-只是一个抬头(。

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;
    }
}

怎么样

#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

我使用了一个简单的函数,该函数从(标准(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);
}
import datetime 
from datetime import date
from dateutil.relativedelta import relativedelta
year = int((date.today()).strftime("%Y"))
month = list(range(1, 13, 1))
YearMonthDay = [(datetime.datetime(year, x, 1) + relativedelta(day=31)).strftime("%Y%m%d") for x in month]
print(YearMonthDay)

【'20220131','20220228','20220331','202 20430','2020 20531','20220630','202 20731','.20220831',''20220930','.20221031','-20221130',''20221231'】

在C++20中:

#include <chrono>
std::chrono::day
get_max_day(std::chrono::month m, std::chrono::year y)
{
    return (y/m/std::chrono::last).day();
}

如果你真的需要一个类型不安全的API:

int
get_max_day(int m, int y)
{
    return unsigned{(std::chrono::last/m/y).day()};
}
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);
相关文章: