首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

请问一个有关问题:两个时间相减,如何得到多天,多少时,多少天分,多少秒

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

请教一个问题:两个时间相减,怎么得到多天,多少时,多少天分,多少秒
如题:例如我有两个时间:
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);

热点排行