c++builder中TDateTime與c中time_t的轉換
c++builder中日期時間控制元件Tdatetimepicker不能同時更改日期和時間,只好用了2個控制元件,其中一個kind屬性改為dtkTime表明是時間,另一個預設是日期,設定2個空間change事件聯動,最後取值與time_t轉換
void __fastcall TmodifyoperationForm::dtpChange(TObject *Sender)
{
if(this->dtp2->Date != this->dtp->Date)
this->dtp2->Date = this->dtp->Date;
}
void __fastcall TmodifyoperationForm::dtp2Change(TObject *Sender)
{
if(this->dtp->Time != this->dtp2->Time)
this->dtp->Time = this->dtp2->Time;
}
TDateTime Time_t_to_TDateTime(time_t t)
{
struct tm *tmWk;
SYSTEMTIME SystemTime;
tmWk = localtime(&t);
SystemTime.wYear = tmWk-> tm_year + 1900;
SystemTime.wMonth = tmWk-> tm_mon + 1;;
SystemTime.wDay = tmWk-> tm_mday;
SystemTime.wHour =tmWk-> tm_hour;
SystemTime.wMinute =tmWk-> tm_min;
SystemTime.wSecond =tmWk-> tm_sec;
SystemTime.wMilliseconds = 0;
return SystemTimeToDateTime(SystemTime);
}
dtp->DateTime=Time_t_to_TDateTime(datetime);//給控制元件賦值
#include <DateUtils.hpp>
time_t t=SecondsBetween( this->dtp->DateTime,25569 );從控制元件得到值