C++ Builder 参考手册 ➙ System::Sysutils ➙ FormatDateTime
日期时间类型数值转字符串
头文件:#include <System.SysUtils.hpp>
命名空间:System::Sysutils
函数原型:
System::UnicodeString __fastcall FormatDateTime(const System::UnicodeString Format, System::TDateTime DateTime);
System::UnicodeString __fastcall FormatDateTime(const System::UnicodeString Format, System::TDateTime DateTime, const TFormatSettings &AFormatSettings);
参数:
- Format:日期和时间的格式;
- DateTime:日期时间类型变量;
- AFormatSettings:地区格式;
返回值:
- 日期时间转字符串的结果通过这个参数返回;
- 参数 DateTime 转为字符串,使用 Format 参数的日期和时间的格式;
- 如果没有 AFormatSettings 参数,函数内部使用全局变量 System::Sysutils::FormatSettings 作为这个参数,所以没有 AFormatSettings 参数的版本不是线程安全的,有 AFormatSettings 参数的版本不使用全局变量,所以是线程安全的;
- 可以使用全局变量 System::Sysutils::FormatSettings 修改默认的格式;
- FormatDateTime 函数通过调用 DateTimeToString 函数实现,他们执行的结果是相同的,区别是 FormatDateTime 生成的字符串作为函数的返回值,而 DateTimeToString 是通过 Result 参数返回的。
Format 参数的格式:
标识符 | 输出 |
---|---|
c | 输出日期格式使用全局变量 FormatSettings 或参数里面的 ShortDateFormat 接下来输出时间采用全局变量 FormatSettings 或参数里面的 LongTimeFormat,如果正好是半夜零时不输出时间 |
d | 输出日 1-31 |
dd | 输出两位数的日,不足两位前面补零 01-31 |
ddd | 输出星期的简称,使用全局变量 FormatSettings 或参数里面的 ShortDayNames 里面保存的星期简称,例如 日、一、二、三 或 Sun、Mon、Tue 等 |
dddd | 输出星期的全称,使用全局变量 FormatSettings 或参数里面的 LongDayNames 里面保存的星期名称,例如 星期日、星期一 或 Sunday、Monday 等 |
ddddd | 输出日期,使用全局变量 FormatSettings 或参数里面的 ShortDateFormat 指定的格式 |
dddddd | 输出日期,使用全局变量 FormatSettings 或参数里面的 LongDateFormat 指定的格式 |
e | 输出本地日历的年,最多两位 (日本、韩国、台湾等地区),只支持 Windows 操作系统 |
ee | 输出本地日历的年,两位数,不足两位前面补零 (日本、韩国、台湾等地区),只支持 Windows 操作系统 |
eeee | 输出本地日历的年,四位数,不足四位前面补零 (日本、韩国、台湾等地区),只支持 Windows 操作系统 |
g | 输出本地日历的纪元简称,例如 "平成","단기" 等 (日本、韩国、台湾等地区),只支持 Windows 操作系统 |
gg | 输出本地日历的纪元全称,例如 "平成","단기" 等 (日本、韩国、台湾等地区),只支持 Windows 操作系统 |
m | 输出月份 1-12。如果 m 紧跟在 h 或 hh 之后,将会输出分钟 0-59 |
mm | 输出两位数的月份,不足两位前面补零 01-12。如果 mm 紧跟在 h 或 hh 之后,将会输出两位数的分钟 00-59 |
mmm | 输出月份的简称,使用全局变量 FormatSettings 或参数里面的 ShortMonthNames 里面保存的月份简称,例如 Jan、Dec 或 一、十二 等 |
mmmm | 输出月份的全称,使用全局变量 FormatSettings 或参数里面的 LongMonthNames 里面保存的月份的全称,例如 January、December 或 一月、十二月 等 |
yy | 输出两位数的年 00-99 |
yyyy | 输出四位数的年 0000-9999 |
h | 输出小时 0-23 |
hh | 输出两位数的小时,不足两位前面补零 00-23 |
n | 输出分钟 0-59 |
nn | 输出两位数的分钟,不足两位前面补零 00-59 |
s | 输出秒 0-59 |
ss | 输出两位数的秒,不足两位前面补零 00-59 |
z | 输出毫秒 0-999 |
zzz | 输出三位数的毫秒,不足三位前面补零 000-999 |
t | 输出时间,使用全局变量 FormatSettings 或参数里面的 ShortTimeFormat 指定的格式 |
tt | 输出时间,使用全局变量 FormatSettings 或参数里面的 LongTimeFormat 指定的格式 |
am/pm | 如果时间在上午,输出 "am";下午输出 "pm",并且格式里面的 h 或 hh 使用十二小时制,大小写与格式里面的标识符 am/pm 相同,全大写、全小写、混合大小写都可以 |
a/p | 如果时间在上午,输出 "a";下午输出 "p",并且格式里面的 h 或 hh 使用十二小时制,大小写与格式里面的标识符 am/pm 相同,全大写、全小写、混合大小写都可以 |
ampm | 如果时间在上午,输出全局变量 FormatSettings 或参数里面的 TimeAMString;下午输出全局变量 FormatSettings 或参数里面的 TimePMString,并且格式里面的 h 或 hh 使用十二小时制,大小写与格式里面的标识符 am/pm 相同,全大写、全小写、混合大小写都可以 |
/ | 输出日期分隔符,使用全局变量 FormatSettings 或参数里面的 DateSeparator 指定的字符,如果要输出 / 本身,需要放在引号里面 |
: | 输出时间分隔符,使用全局变量 FormatSettings 或参数里面的 TimeSeparator 指定的字符,如果要输出 : 本身,需要放在引号里面 |
'xx' 或 "xx" | 单引号或双引号里面的内容原样输出 |
例:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
System::TDateTime dt = Sysutils::Now();
Memo1->Lines->Add(Sysutils::FormatDateTime(L"yyyy/mm/dd", dt));
TFormatSettings fs = TFormatSettings::Create();
fs.DateSeparator = L'-';
Memo1->Lines->Add(Sysutils::FormatDateTime(L"yyyy/mm/dd", dt, fs));
Memo1->Lines->Add(Sysutils::FormatDateTime(L"yyyy'年'mm'月'dd'日' hh:nn:ss.zzz", dt));
}
运行结果:
相关:
- System::Sysutils::FormatSettings
- System::Sysutils::TFormatSettings
- System::Sysutils::StrToBool
- System::Sysutils::StrToBoolDef
- System::Sysutils::TryStrToBool
- System::Sysutils::BoolToStr
- System::Sysutils::CurrToStr
- System::Sysutils::CurrToStrF
- System::Sysutils::DateTimeToStr
- System::Sysutils::DateTimeToString
- System::Sysutils::DateToStr
- System::Sysutils::FloatToStr
- System::Sysutils::FloatToStrF
- System::Sysutils::GUIDToString
- System::Sysutils::IntToStr
- System::Sysutils::IntToHex
- System::Sysutils::TimeToStr
- System::Sysutils::UIntToStr
- System::Sysutils
- System::Dateutils
- System
- std::itoa, std::_itoa, std::_itot, std::_itow
- std::ltoa, std::_ltoa, std::_ltot, std::_ltow
- std::ultoa, std::_ultoa, std::_ultot, std::_ultow
- std::_i64toa, std::_i64tot, std::_i64tow
- std::_ui64toa, std::_ui64tot, std::_ui64tow
- std::ecvt, std::_ecvt
- std::fcvt, std::_fcvt
- std::gcvt, std::_gcvt
- <cstdlib>
C++ Builder 参考手册 ➙ System::Sysutils ➙ FormatDateTime