C++ Builder 参考手册 ➙ System::Sysutils ➙ StrToDateDef
字符串转日期类型数值
头文件:#include <System.SysUtils.hpp>
命名空间:System::Sysutils
函数原型:
System::TDateTime __fastcall StrToDateDef(
const System::UnicodeString S,
const System::TDateTime Default);
System::TDateTime __fastcall StrToDateDef(
const System::UnicodeString S,
const System::TDateTime Default,
const TFormatSettings &AFormatSettings);
参数:
- S:字符串;
- Default:默认值;
- AFormatSettings:地区格式;
返回值:
- 日期时间类型数值,如果转换失败,函数返回参数 Default 值;
- 字符串 S 必须是日期,不能包含时间;
- 如果没有 AFormatSettings 参数,日期格式必须和全局变量 FormatSettings.ShortDateFormat 相同,分隔符必须是 FormatSettings.DateSeparator,请参考本文例子;
如果有 AFormatSettings 参数,日期格式必须和参数 AFormatSettings.ShortDateFormat 相同,分隔符必须是 AFormatSettings.DateSeparator,请参考本文例子; - 日期时间格式可以参考 System::Sysutils::FormatDateTime;
- 函数 StrToDate、StrToDateDef 和 TryStrToDate 的区别:
• StrToDate 转换失败抛出异常;
• StrToDateDef 转换失败返回默认值;
• TryStrToDate 转换结果通过参数返回,函数返回值返回是否转换成功; - 没有 AFormatSettings 参数的 CurrToStrF 函数不是线程安全的,因为使用了全局变量 FormatSettings 作为默认的地区格式;带有 AFormatSettings 参数的函数是线程安全的。
例:
测试默认日期格式和通过 Sysutils::FormatSettings 修改默认日期格式,
通过 Sysutils::GetFormatSettings 恢复默认格式。
通过函数参数 AFormatSettings 指定格式请参考 StrToDate 的例子。
void TForm1::ShowDate(TDateTime dt)
{
Memo1->Lines->Add(FormatDateTime(L"yyyy'年'm'月'd'日'", dt)); // 显示日期
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TDateTime dtDefault = Sysutils::Now(); // 默认值为当前日期
ShowDate(StrToDateDef(L"2022/03/31", dtDefault)); // 中国默认日期格式为 年/月/日
ShowDate(StrToDateDef(L"2022-03-31", dtDefault)); // 转换失败,返回默认值
ShowDate(StrToDateDef(L"03-31-2022", dtDefault)); // 转换失败,返回默认值
ShowDate(StrToDateDef(L"03/31/2022", dtDefault)); // 转换失败,返回默认值
Memo1->Lines->Add(L"-");
Sysutils::FormatSettings.DateSeparator = L'-'; // 把默认的日期分隔符改为减号 '-'
Sysutils::FormatSettings.ShortDateFormat = L"yyyy/mm/dd"; // 继续修改默认日期格式为 年/日/月
ShowDate(StrToDateDef(L"2022/03/31", dtDefault)); // 转换失败,返回默认值
ShowDate(StrToDateDef(L"2022-03-31", dtDefault)); // 符合修改之后的格式,转换成功
ShowDate(StrToDateDef(L"03-31-2022", dtDefault)); // 转换失败,返回默认值
ShowDate(StrToDateDef(L"03/31/2022", dtDefault)); // 转换失败,返回默认值
Memo1->Lines->Add(L"-");
Sysutils::FormatSettings.ShortDateFormat = L"mm/dd/yyyy"; // 继续修改默认日期格式为 日/月/年
ShowDate(StrToDateDef(L"2022/03/31", dtDefault)); // 转换失败,返回默认值
ShowDate(StrToDateDef(L"2022-03-31", dtDefault)); // 转换失败,返回默认值
ShowDate(StrToDateDef(L"03-31-2022", dtDefault)); // 符合修改之后的格式,转换成功
ShowDate(StrToDateDef(L"03/31/2022", dtDefault)); // 转换失败,返回默认值
Memo1->Lines->Add(L"-");
Sysutils::GetFormatSettings(); // 格式恢复默认值 - 当前地区 (中国) 的格式
ShowDate(StrToDateDef(L"2022/03/31", dtDefault)); // 中国默认日期格式为 年/月/日
ShowDate(StrToDateDef(L"2022-03-31", dtDefault)); // 转换失败,返回默认值
ShowDate(StrToDateDef(L"03-31-2022", dtDefault)); // 转换失败,返回默认值
ShowDate(StrToDateDef(L"03/31/2022", dtDefault)); // 转换失败,返回默认值
}
运行结果:
剪貼簿01.png
相关:
- System::Sysutils::DateTimeToStr
- System::Sysutils::DateTimeToString
- System::Sysutils::DateToStr
- System::Sysutils::FormatDateTime
- System::Sysutils::StrToBool
- System::Sysutils::StrToBoolDef
- System::Sysutils::StrToCurr
- System::Sysutils::StrToCurrDef
- System::Sysutils::StrToDate
- System::Sysutils::StrToDateDef
- System::Sysutils::StrToDateTime
- System::Sysutils::StrToDateTimeDef
- System::Sysutils::StrToFloat
- System::Sysutils::StrToFloatDef
- System::Sysutils::StrToInt
- System::Sysutils::StrToIntDef
- System::Sysutils::StrToInt64
- System::Sysutils::StrToInt64Def
- System::Sysutils::StrToTime
- System::Sysutils::StrToTimeDef
- System::Sysutils::StrToUInt
- System::Sysutils::StrToUIntDef
- System::Sysutils::StrToUInt64
- System::Sysutils::StrToUInt64Def
- System::Sysutils::TimeToStr
- System::Sysutils::TryStrToBool
- System::Sysutils::TryStrToCurr
- System::Sysutils::TryStrToDate
- System::Sysutils::TryStrToDateTime
- System::Sysutils::TryStrToFloat
- System::Sysutils::TryStrToInt
- System::Sysutils::TryStrToInt64
- System::Sysutils::TryStrToTime
- System::Sysutils::TryStrToUInt
- System::Sysutils::TryStrToUInt64
- System::Sysutils
- std::atof, std::_ttof, std::_wtof
- std::_atold, std::_ttold, std::_wtold
- std::atoi, std::_ttoi, std::_wtoi
- std::atol, std::_ttol, std::_wtol
- std::atoll, std::_ttoll, std::_wtoll
- std::_atoi64, std::_ttoi64, std::_wtoi64
- std::strtof, std::_tcstof, std::wcstof
- std::strtod, std::_tcstod, std::wcstod
- std::strtold, std::wcstold, std::_strtold, std::_tcstold, std::_wcstold
- std::strtol, std::_tcstol, std::wcstol
- std::strtoll, std::_tcstoll, std::wcstoll
- std::strtoul, std::_tcstoul, std::wcstoul
- std::strtoull, std::_tcstoull, std::wcstoull
- <cstdlib>
C++ Builder 参考手册 ➙ System::Sysutils ➙ StrToDateDef