C++中显示当前时间的方法:

来源:互联网 发布:免费淘宝店招素材图片 编辑:程序博客网 时间:2024/06/01 18:29
C++中显示当前时间的方法:
#include <iostream>
#include <ctime> // 系统时间函数
using namespace std;

int main()
{
    time_t now=time(0);
cout<<asctime(gmtime(&now));
   
    return 0;
}

注意这个显示的时间是格林威治时间。当地时间如何转化我还不知道,有知道的可以回帖说明一下,谢谢!

本文转自http://hi.baidu.com/yanerxh/blog/item/fe578124ad515d36c9955985.html


或者

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    time_t t;
    struct tm *pnow = NULL;

    t = time(&t);
    pnow = localtime(&t);

     cout << pnow->tm_year + 1900 << "年" << pnow->tm_mon + 1 <<"月" << pnow->tm_mday <<"日"<<endl;
     cout << pnow->tm_hour <<"时"<< pnow->tm_min <<"分"<< pnow->tm_sec <<"秒"<<endl<<endl;

     //或者

  cout << asctime(pnow) <<endl;
return 0;
}


原创粉丝点击