CTime::Format("%H:%M:%S") 輸出 23:8:48,分鐘缺前導零。
CString strTime = time.Format(_T("%H:%02M:%S")); // MFC CTime
// 或
CString strTime;
strTime.Format(_T("%02d:%02d:%02d"), hour, minute, second); // 通用 Format
| 規格符 | 含義 | 範例 |
|---|---|---|
| %H | 24小時制 (0-23) | 23 |
| %02H | 同上,強制兩位 | 08 |
| %M | 分鐘 (0-59) | 8 |
| %02M | 分鐘兩位 | 08 |
| %S | 秒 (0-59) | 48 |
#include <iomanip>
cout << setfill('0') << setw(2) << hour << ':'
<< setw(2) << minute << ':'
<< setw(2) << second;