CTime时间转换为字符串2025-06-11 08:52:54的几种方法

在 MFC 或 Windows 编程中,CTime 类提供了多种方式将时间对象转换为特定格式的字符串。以下是几种将 CTime 转换为 "2025-06-11 08:52:54" 格式的方法:

方法一:使用 Format 方法

CTime 类自带的 Format 方法是最直接的方式,可以使用格式控制符:

cpp

运行

CTime time(2025, 6, 11, 8, 52, 54);
CString strTime = time.Format("%Y-%m-%d %H:%M:%S");
方法二:使用 FormatGmt 方法 (格林威治时间)

如果需要显示 GMT 时间:

cpp

运行

CTime time(2025, 6, 11, 8, 52, 54);
CString strTime = time.FormatGmt("%Y-%m-%d %H:%M:%S");
方法三:分步构建字符串

如果需要更多的自定义控制,可以分步构建:

cpp

运行

CTime

你可能感兴趣的:(c++编程,c++,开发语言,算法,c语言,git,visual,studio,链表)