C++获取当前时间(年月日、时分秒、毫秒)

获取时间:
年-月-日(YYmmdd)
时:分:秒(HHMMSS)
毫秒(MS)

效率问题需要再优化
代码:

#include 
#include 
#include 
using namespace std;

struct NowDate
{
    char tmp0[16]; //年月日
    char tmp1[16]; //时分秒
    char tmp2[4];  //毫秒
};

NowDate getTime()
{
    time_t timep;
    time (&timep);
    NowDate date;

    strftime(date.tmp0, sizeof(date.tmp0), "%Y-%m-%d",localtime(&timep) );
    strftime(date.tmp1, sizeof(date.tmp1), "%H:%M:%S",localtime(&timep) );

    struct timeb tb;
    ftime(&tb);     
    sprintf(date.tmp2,"%d",tb.millitm);

    return date;
}

int main()
{
    NowDate time = getTime();
    cout << time.tmp0 <cout << time.tmp1 <cout << time.tmp2 <return 0;
}

结果:
C++获取当前时间(年月日、时分秒、毫秒)_第1张图片

你可能感兴趣的:(C++)