读书人

请问一个有关问题:两个时间相减如何

发布时间: 2013-04-20 19:43:01 作者: rapoo

请教一个问题:两个时间相减,怎么得到多天,多少时,多少天分,多少秒
如题:例如我有两个时间:
Timer1:“2020-12-03 23:59:59”,
Timer2:“2013-04-08 08:00:00”

Timer1 - Timer2 得到的时间,怎么获知是多天,多少时,多少天分,多少秒?
[解决办法]

AnsiString sStr2 = "2013-04-08 08:00:00";
AnsiString sStr2 = "2020-12-03 23:59:59";
int nDay = StrToDateTime(sStr2) - StrToDateTime(sStr); //天
int nHours = double(StrToDateTime(sStr2) - StrToDateTime(sStr)) * 24; //小时
int nMin = double(StrToDateTime(sStr2) - StrToDateTime(sStr)) * 24 * 60; //分钟
int nSec = double(StrToDateTime(sStr2) - StrToDateTime(sStr)) * 24 * 60 * 60;//秒

[解决办法]
#include <DateUtils.hpp>

用下面这些现成的函数
int __fastcall YearsBetween(const System::TDateTime ANow, const System::TDateTime AThen);
int __fastcall MonthsBetween(const System::TDateTime ANow, const System::TDateTime AThen);
int __fastcall WeeksBetween(const System::TDateTime ANow, const System::TDateTime AThen);
int __fastcall DaysBetween(const System::TDateTime ANow, const System::TDateTime AThen);
__int64 __fastcall HoursBetween(const System::TDateTime ANow, const System::TDateTime AThen);
__int64 __fastcall MinutesBetween(const System::TDateTime ANow, const System::TDateTime AThen);
__int64 __fastcall SecondsBetween(const System::TDateTime ANow, const System::TDateTime AThen);
__int64 __fastcall MilliSecondsBetween(const System::TDateTime ANow, const System::TDateTime AThen);

读书人网 >C++ Builder

热点推荐