在C ++中以YYYYMMDD格式获取月份的最后一个日期 [英] Get the last date of month in YYYYMMDD format in C++

查看:104
本文介绍了在C ++中以YYYYMMDD格式获取月份的最后一个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前在我的代码中,我将从文件中读取一些日期,如'CString tsDate',格式为YYYYMMDD(例如:20180501)

我需要检查'tsDate'是否是最后一个那个月的那一天。这意味着5月份它是20180531所以我得到了这个日期。



我的代码目前正在使用COleDateTime类来获取GetMonth()和GetYear。 />


有谁能建议我得到这个月的最后一个日期?



我试过的:



我试过以下内容:

int getNumberOfDays(int month,int year)

{

//闰年条件,如果月份是2

if(month == 2)

{

if((年%400 == 0)||(年%4 == 0&年%100!= 0))

返回29;

其他

返回28;

}

//月份有31天

否则如果(月== 1 ||月== 3 ||月== 5 ||月== 7 ||月== 8

||月== 10 ||月== 12)

返回31;

其他

返回30;

}



但是这几个月是硬编码的,但每年的天数可能会有所不同ght?

Currently in my code i will get some date from the file reading like 'CString tsDate' in the format of YYYYMMDD(ex: 20180501)
and i need to check whether 'tsDate' is last day of that month. It means for MAY month it is 20180531 so i nned to get this date.

In my code currently am using COleDateTime class to get GetMonth() and GetYear.

Can anyone suggests me to get the last date of the month?

What I have tried:

I have tried the below :
int getNumberOfDays(int month, int year)
{
//leap year condition, if month is 2
if( month == 2)
{
if((year%400==0) || (year%4==0 && year%100!=0))
return 29;
else
return 28;
}
//months which has 31 days
else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8
||month == 10 || month==12)
return 31;
else
return 30;
}

But here months are hardcoded but every year number of days may vary right?

推荐答案

引用:

但是这几个月都是硬编码但是每年都有数字天数可能会有所不同吗?

But here months are hardcoded but every year number of days may vary right?

只是2月份的天数可能会有所不同,无论如何,已发布的代码都会正确处理它。



另外,您还可以添加一天到您的 COleDateTime 对象(使用1天 COleDateTimeSpan 对象)并查看是否有月份更改。

Just February days may vary, anyway the posted code correctly handles it.

As an alternative, you could also add a day to your COleDateTime object (using a 1-day COleDateTimeSpan object) and see if there is a month change.


引用:

但这几个月是硬编码的,但每年的天数可能会有所不同吗?

But here months are hardcoded but every year number of days may vary right?



怎么可能不知道阳历如何运作并且代码正确?

这个问题让我们想知道你是否只知道它是如何运作的您在互联网上找不到:

公历 - 维基百科 [ ^ ]


这篇关于在C ++中以YYYYMMDD格式获取月份的最后一个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆