您好,欢迎来到刀刀网。
搜索
您的当前位置:首页C++获取当前时间(年月日、时分秒、毫秒)

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

来源:刀刀网
版权声明:本文为博主原创文章,未经博主允许不得转载。 https:///mengxiangjia_linxi/article/details/807284
                                        <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

结果:



因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- gamedaodao.com 版权所有 湘ICP备2022005869号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务