版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mengxiangjia_linxi/article/details/80647284
<div class="markdown_views">
<!-- flowchart 箭头图标 勿删 -->
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg>
<p><strong>获取时间:</strong> <br>
年-月-日(YYmmdd)
时:分:秒(HHMMSS)
毫秒(MS)
效率问题需要再优化
代码:
#include <iostream>
#include <string>
#include <time.h>
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 <<endl;
cout << time.tmp1 <<endl;
cout << time.tmp2 <<endl;
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
结果: