读书人

关于日期时间的有关问题

发布时间: 2012-02-13 17:20:26 作者: rapoo

关于日期时间的问题


int getTimeCount(TDateTime start, TDateTime end)
{

return end-start ;
// 也就是说 我想得到结束日期 减去开始日期 结果的 总共的秒数

}


问题 1: return result=end-start //结果单位为 秒.

for examper
end=2007-08-21-13:44:15;
start =2007-08-21-13:44:10;

result=5 秒.

  注意:两个日期时间变量直接相减不能得到正确的结果.


问题 2:


一个日期时间变量直接相加N秒 得到的结果不对,如何解决

[解决办法]
The TDateTime class inherits a val data member declared as a double that holds the date-time value. The integral part of a TDateTime value is the number of days that have passed since 12/30/1899. The fractional part of a TDateTime value is the time of day.

所以你日期相同时相减取整的结果就是0

要得到相差的秒数可以用
extern PACKAGE __int64 __fastcall SecondsBetween(const System::TDateTime ANow, const System::TDateTime AThen);

加N秒用
extern PACKAGE System::TDateTime __fastcall IncSecond(const System::TDateTime AValue, const__int64 ANumberOfSeconds = 0x000000001);

注意,以上2函数需要
#include <DateUtils.hpp>


[解决办法]
TDateTime可以看作是一个double类型,小数点位置是在天的后面,也就是说两个时间相减结果是天数(double)。


TDateTime a=StrToDateTime("2007-08-21 13:44:15");
TDateTime b=StrToDateTime("2007-08-21 13:44:10");
//ShowMessage(a-b);//结果为"00:00:05"
double c=a-b;
int d=RoundTo(c*24*69*69,0);//天转换为秒,取整//#include<math.hpp>
ShowMessage(d);//结果为"5";

[解决办法]
同意老蔡
#include <DateUtils.hpp >
用里面的DaysBetween SecondsBetween 等等函数 直接去取返回值

读书人网 >C++ Builder

热点推荐