我试图写一个自己的时区转换器,我需要一种方法来确定这个月的最后一天是什么时候。经过一番研究,我发现了寻找闰年的公式。问与答:如何确定每月的最后一天是什么?
这是一个小小的贡献,但也许我会拯救别人20分钟,它让我找出并应用它。
此代码接受一个已签名的短月份,索引编号为0(0表示1月),同时也表示索引为0的int年份(2012为2012)。
它返回一个索引日(27日是第27日,但在SYSTEMTIME结构等中,您通常需要索引0 - 只是一个头)。
我试图写一个自己的时区转换器,我需要一种方法来确定这个月的最后一天是什么时候。经过一番研究,我发现了寻找闰年的公式。问与答:如何确定每月的最后一天是什么?
这是一个小小的贡献,但也许我会拯救别人20分钟,它让我找出并应用它。
此代码接受一个已签名的短月份,索引编号为0(0表示1月),同时也表示索引为0的int年份(2012为2012)。
它返回一个索引日(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
这太过分了。 – 2013-03-26 07:01:37
有点点!但与多个ifss相比,这是一个干净的解决方案,并检查闰年的不同规则等。 – Saqlain 2013-03-26 07:06:01
诚然,有两个可能的答案,但没有问题!只是给你更多的方法来解决问题。 – 2013-04-17 01:11:11
我用一个简单的函数,从一个返回在整个日期(标准)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);
}
你可以发布代码的一些解释吗?将有助于明确您的答案... – NREZ 2013-08-27 10:04:39
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);
'如果(!一年%4 == 0 &&年400%= 0)返回29,否则返回28'是一种更简洁的方式表达我的想法。 – john 2013-03-26 06:42:14
@john简洁,但错误。 – 2013-03-26 06:53:01
@AlexeyFrunze请提供更正或反例。 – john 2013-03-26 06:58:09