C++ Builder 参考手册 ➙ System::Sysutils ➙ EncodeDate
由年、月、日生成日期时间类型数值
头文件:#include <System.SysUtils.hpp>
命名空间:System::Sysutils
函数原型:
System::TDateTime __fastcall EncodeDate(System::Word Year, System::Word Month, System::Word Day);
参数:
- Year:年,1 ~ 9999
- Month:月,1 ~ 12
- Day:日,1 ~ 31
返回值:
- 日期时间类型数值,日期为参数指定的年、月、日,时间为 00:00:00;
- 如果参数指定的年、月、日无效,抛出 EConvertError 异常。
例:2021年10月29日转换为日期时间成功,2021年2月29日转换为日期时间失败:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
try
{
TDateTime dt = Sysutils::EncodeDate(2021, 10, 29);
Memo1->Lines->Add(FormatDateTime(L"yyyy/mm/dd", dt));
}
catch(Exception &e)
{
ShowMessage(L"转换出错:" + e.Message);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
try
{
TDateTime dt = Sysutils::EncodeDate(2021, 2, 29);
Memo1->Lines->Add(FormatDateTime(L"yyyy/mm/dd", dt));
}
catch(Exception &e)
{
ShowMessage(L"转换出错:" + e.Message);
}
}
运行结果:
相关:
- System::Dateutils::DecodeDateTime
- System::Dateutils::EncodeDateTime
- System::Dateutils::TryEncodeDateTime
- System::Dateutils::DateTimeToUnix
- System::Dateutils::UnixToDateTime
- System::Dateutils::YearOf
- System::Dateutils::MonthOf
- System::Dateutils::DayOf
- System::Dateutils::DayOfTheWeek
- System::Dateutils::DayOfTheMonth
- System::Dateutils::DayOfTheYear
- System::Dateutils::HourOf
- System::Dateutils::MinuteOf
- System::Dateutils::SecondOf
- System::Dateutils::MilliSecondOf
- System::Dateutils
- System::Sysutils::DecodeDate
- System::Sysutils::DecodeDateFully
- System::Sysutils::DecodeTime
- System::Sysutils::EncodeDate
- System::Sysutils::EncodeTime
- System::Sysutils::TryEncodeDate
- System::Sysutils::TryEncodeTime
- System::Sysutils::TryStrToDate
- System::Sysutils::TryStrToDateTime
- System::Sysutils::TryStrToTime
- System::Sysutils::FormatDateTime
- System::Sysutils::CurrentYear
- System::Sysutils::Now
- System::Sysutils::Date
- System::Sysutils::Time
- System::Sysutils
C++ Builder 参考手册 ➙ System::Sysutils ➙ EncodeDate