c++ 获取系统时间转换为字符串,整型转换为字符换

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

string get_time_str() //return a string of time
{
    time_t ts = time(NULL);
    tm time = *localtime(&ts);
    int year = time.tm_year + 1900;
    int month = time.tm_mon + 1;
    int day = time.tm_mday;
    int hour = time.tm_hour;
    int min = time.tm_min;
    int sec = time.tm_sec;
    int week = time.tm_wday ;

    stringstream s;
    s <<year<<"-"<<month<<"-"<<day<<"-"<<hour<<":"<<min<<":"<<sec;
    string s1 = s.str();
    return s1;
}

string int2string(int i)
{
    stringstream s;
    s << i;
    string ss = s.str();
    return ss;
}

int main()
{
    string s1 = int2string(10);
    cout<<s1<<endl;
    s1=get_time_str();
    cout<<s1<<endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/yuan_chen_/article/details/81259434
今日推荐