FormatDateTime - C++ Builder

C++ Builder 參考手冊 ? System::Sysutils ? FormatDateTime


日期時間類型數(shù)值轉(zhuǎn)字符串

頭文件:#include <System.SysUtils.hpp>
命名空間:System::Sysutils
函數(shù)原型:

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);

參數(shù):

  • Format:日期和時間的格式;
  • DateTime:日期時間類型變量;
  • AFormatSettings:地區(qū)格式;

返回值:

  • 日期時間轉(zhuǎn)字符串的結(jié)果通過這個參數(shù)返回;
  • 參數(shù) DateTime 轉(zhuǎn)為字符串,使用 Format 參數(shù)的日期和時間的格式;
  • 如果沒有 AFormatSettings 參數(shù),函數(shù)內(nèi)部使用全局變量 System::Sysutils::FormatSettings 作為這個參數(shù),所以沒有 AFormatSettings 參數(shù)的版本不是線程安全的,有 AFormatSettings 參數(shù)的版本不使用全局變量,所以是線程安全的;
  • 可以使用全局變量 System::Sysutils::FormatSettings 修改默認的格式;
  • FormatDateTime 函數(shù)通過調(diào)用 DateTimeToString 函數(shù)實現(xiàn),他們執(zhí)行的結(jié)果是相同的,區(qū)別是 FormatDateTime 生成的字符串作為函數(shù)的返回值,而 DateTimeToString 是通過 Result 參數(shù)返回的。

Format 參數(shù)的格式:

標(biāo)識符 輸出
c 輸出日期格式使用全局變量 FormatSettings 或參數(shù)里面的 ShortDateFormat 接下來輸出時間采用全局變量 FormatSettings 或參數(shù)里面的 LongTimeFormat,如果正好是半夜零時不輸出時間
d 輸出日 1-31
dd 輸出兩位數(shù)的日,不足兩位前面補零 01-31
ddd 輸出星期的簡稱,使用全局變量 FormatSettings 或參數(shù)里面的 ShortDayNames 里面保存的星期簡稱,例如 日、一、二、三 或 Sun、Mon、Tue 等
dddd 輸出星期的全稱,使用全局變量 FormatSettings 或參數(shù)里面的 LongDayNames 里面保存的星期名稱,例如 星期日、星期一 或 Sunday、Monday 等
ddddd 輸出日期,使用全局變量 FormatSettings 或參數(shù)里面的 ShortDateFormat 指定的格式
dddddd 輸出日期,使用全局變量 FormatSettings 或參數(shù)里面的 LongDateFormat 指定的格式
e 輸出本地日歷的年,最多兩位 (日本、韓國、臺灣等地區(qū)),只支持 Windows 操作系統(tǒng)
ee 輸出本地日歷的年,兩位數(shù),不足兩位前面補零 (日本、韓國、臺灣等地區(qū)),只支持 Windows 操作系統(tǒng)
eeee 輸出本地日歷的年,四位數(shù),不足四位前面補零 (日本、韓國、臺灣等地區(qū)),只支持 Windows 操作系統(tǒng)
g 輸出本地日歷的紀(jì)元簡稱,例如 "平成","??" 等 (日本、韓國、臺灣等地區(qū)),只支持 Windows 操作系統(tǒng)
gg 輸出本地日歷的紀(jì)元全稱,例如 "平成","??" 等 (日本、韓國、臺灣等地區(qū)),只支持 Windows 操作系統(tǒng)
m 輸出月份 1-12。如果 m 緊跟在 h 或 hh 之后,將會輸出分鐘 0-59
mm 輸出兩位數(shù)的月份,不足兩位前面補零 01-12。如果 mm 緊跟在 h 或 hh 之后,將會輸出兩位數(shù)的分鐘 00-59
mmm 輸出月份的簡稱,使用全局變量 FormatSettings 或參數(shù)里面的 ShortMonthNames 里面保存的月份簡稱,例如 Jan、Dec 或 一、十二 等
mmmm 輸出月份的全稱,使用全局變量 FormatSettings 或參數(shù)里面的 LongMonthNames 里面保存的月份的全稱,例如 January、December 或 一月、十二月 等
yy 輸出兩位數(shù)的年 00-99
yyyy 輸出四位數(shù)的年 0000-9999
h 輸出小時 0-23
hh 輸出兩位數(shù)的小時,不足兩位前面補零 00-23
n 輸出分鐘 0-59
nn 輸出兩位數(shù)的分鐘,不足兩位前面補零 00-59
s 輸出秒 0-59
ss 輸出兩位數(shù)的秒,不足兩位前面補零 00-59
z 輸出毫秒 0-999
zzz 輸出三位數(shù)的毫秒,不足三位前面補零 000-999
t 輸出時間,使用全局變量 FormatSettings 或參數(shù)里面的 ShortTimeFormat 指定的格式
tt 輸出時間,使用全局變量 FormatSettings 或參數(shù)里面的 LongTimeFormat 指定的格式
am/pm 如果時間在上午,輸出 "am";下午輸出 "pm",并且格式里面的 h 或 hh 使用十二小時制,大小寫與格式里面的標(biāo)識符 am/pm 相同,全大寫、全小寫、混合大小寫都可以
a/p 如果時間在上午,輸出 "a";下午輸出 "p",并且格式里面的 h 或 hh 使用十二小時制,大小寫與格式里面的標(biāo)識符 am/pm 相同,全大寫、全小寫、混合大小寫都可以
ampm 如果時間在上午,輸出全局變量 FormatSettings 或參數(shù)里面的 TimeAMString;下午輸出全局變量 FormatSettings 或參數(shù)里面的 TimePMString,并且格式里面的 h 或 hh 使用十二小時制,大小寫與格式里面的標(biāo)識符 am/pm 相同,全大寫、全小寫、混合大小寫都可以
/ 輸出日期分隔符,使用全局變量 FormatSettings 或參數(shù)里面的 DateSeparator 指定的字符,如果要輸出 / 本身,需要放在引號里面
: 輸出時間分隔符,使用全局變量 FormatSettings 或參數(shù)里面的 TimeSeparator 指定的字符,如果要輸出 : 本身,需要放在引號里面
'xx' 或 "xx" 單引號或雙引號里面的內(nèi)容原樣輸出

例:

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));
}

運行結(jié)果:

運行結(jié)果

相關(guān):

  • 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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容