在C++中,strftime函数用于将时间日期格式化为字符串。其函数原型如下:
size_t strftime(char* str, size_t count, const char* format, const struct tm* timeptr);参数说明:
str:指向存储格式化后字符串的缓冲区count:缓冲区的大小format:格式化字符串的格式timeptr:指向tm结构体的指针,包含要格式化的时间日期信息示例代码如下:
#include <iostream>#include <ctime>int main() { char buffer[80]; const char* format = "%Y-%m-%d %H:%M:%S"; // 日期时间格式 time_t rawtime; struct tm* timeinfo; time(&rawtime); timeinfo = localtime(&rawtime); strftime(buffer, 80, format, timeinfo); std::cout << "当前时间为:" << buffer << std::endl; return 0;}在上面的示例中,我们使用了strftime函数将当前时间格式化为指定的字符串格式并存储在buffer中,然后将其输出到控制台。




