C++ 日期时间使用

#include <time.h>  
#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <sstream>
using namespace std;

int main()  
{  
    time_t now = time(0);  
    cout << now << endl;

    char* date = ctime(&now); //时间转为字符串
    cout << date ;

    struct tm* p = localtime(&now); /*转换为struct tm结构的UTC时间*/  
    printf("%d/%d/%d 
", 1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday);  

    //int 转为 string
    stringstream stream;
    stream << 1900 + p->tm_year;
    string year = stream.str();
    cout << year <<endl;

    return 0;  
}

  

原文地址:https://www.cnblogs.com/nanqiang/p/8668724.html