简单类型 / 日期和时间

#include <iostream>
using namespace std;

time_t now = time(0);
auto tm = localtime(&now);

char shortStyle[20];
strftime(shortStyle, 20"%D %H:%M %p"tm);
//shortStyle: 11/12/23 21:14 PM

char customStyle[20];
strftime(customStyle, 20"%Y-%m-%d"tm);
//customStyle is "2016-05-18"

cout << "shortStyle: "<< shortStyle << endl;
cout << "customStyle: "<< customStyle;

//Visual Studio example
#include <string>
#include <time.h>
using namespace std;

string timeToString(time_t time) {
    char customStyle[20];
    struct tm timeInfo;
    localtime_s(&timeInfo, &time);
    strftime(customStyle, sizeof(customStyle),
        "%Y-%m-%d %H:%M", &timeInfo);
    return string(customStyle);
}