繁体   English   中英

如何使用C ++ Win32 API计算两个日期之间的天数?

[英]How to calculate days between two dates using C++ Win32 API?

我正在使用C ++ win32 API ...

我有这些价值观。

pwdlastset日期(例如:25-9-2012),当前日期(例如:1-11-2012),maxpwdage计数(前54天)pwdwarningdays(14天)...

现在我要计算密码到期日期...

我试过以下代码......

if(lastpwdchmon==currentMonth)
                        {
                        lCount=currentDay-lastpwdchday;
                        }
                        else if(lastpwdchmon<currentMonth)
                        {
                            lCount=((currentDay+30)-lastpwdchday);
                        }

但是,我有一个问题......

我的意思是,我只需要计算当前日期和pwdlastset日期之间的天数?

怎么做到这一点?

很难知道你的DATE是什么,但如果你已经把它们全部搞定了,那么只需减去两个并将结果除以86400(60 * 60 * 24)。

DATE是用于保存日期/时间的COM方法。 它的整体部分包含一个时代的天数(这里无关紧要),小数部分表示一天的时间。 因此,要计算2 DATE之间的天数,您可以执行以下操作:

DATE d1 = get_date1(), d2 = get_date2();
int number_of_days = static_cast<int>( d1 - d2 );

要将当前日期设为DATE您可以使用:

DATE get_now( bool asUTC = false ) {
    SYSTEMTIME stm;
    (asUTC ? ::GetSystemTime : ::GetLocalTime)( &stm );
    DATE res;
    SystemTimeToVariantTime( &stm, &res );
    return res;
}

要将日期/时间字段转换为DATE您可以使用:

DATE to_date( int year, WORD month, WORD day,
    WORD h = 0, WORD m = 0, WORD s = 0, WORD ms = 0 )
{
    SYSTEMTIME stm = { year, month, 0, day, h, m, s, ms };
    DATE res;
    if( !SystemTimeToVariantTime(&stm, &res) ) {/* Handle error */}
    return res;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM