问答:我如何知道本月的最后一天是什么?
Q&A: How do I figure out what the last day of the month is?
我试图编写一个滚动您自己的时区转换器,我需要一种方法来确定一个月的最后一天是什么。经过一番研究,我发现了闰年的计算公式。
这是一个小小的贡献,但也许我会把我花在计算和应用它上的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);
相关文章:
- 在 c++ 中拥有一组结构的正确方法是什么?
- 在 OpenCV C++ 中估计基本矩阵之前对相应点进行归一化的正确方法是什么?
- C++ Chrono 确定一天是否是周末?
- 具有相同特征的两个对象是否只在内存中存储一次?无论定义它们的函数是什么,都是不同的
- 我正在尝试制作一个程序,在添加 n 天(整数)后告诉一个人什么是一天(例如星期一等)
- 在 c++ 或 python 中生成一个体面的视差图以在 Raspberry Pi 上实现的最佳方法(算法或函数)是什么
- 将一种数据类型的向量复制到同一数据类型的结构向量中的有效方法是什么
- 获取英特尔 Kaby Lake 架构上最后一级高速缓存未命中计数的确切代码是什么?
- [basic.lookup]/1 中最后一句话是什么意思?
- 在C++编程中继续下一行的另一种方法是什么?
- 打印矢量时最后的'%'是什么?
- 一个月的最后一天?
- 如何知道您输入到std::array中的最后一个元素是什么?
- 改进malloc()算法的下一步是什么
- 我读了map.erase(map.end());删除地图的最后一个元素。但是最后一个元素是什么?它是否基于元素的插入顺序?
- 问答:我如何知道本月的最后一天是什么?
- 学习C++,下一步是什么?还有什么是推荐的编译器?
- ' std::sin '的最后一位是错误的
- 在C++中使用 Scripting.FileSystemObject 时,#import 后的下一步是什么?
- 当一个c++程序在Windows上终止时,终止存根调用的最后一个函数是什么?