本文将用c++写一个输出现在时间的代码

废话不多说

上代码

#include <iostream>
#include <ctime>
#include <windows.h>
#include <bits/stdc++.h>
using namespace std;
int main()
{
   for( ; ; )
   {
       // 基于当前系统的当前日期/时间
       time_t now = time(0);
   
       // 把 now 转换为字符串形式
       char* dt = ctime(&now);
 
       cout << "本地日期和时间:" << dt << endl;
 
       // 把 now 转换为 tm 结构
       tm *gmtm = gmtime(&now);
       dt = asctime(gmtm);
       cout << "UTC 日期和时间:"<< dt << endl;
       Sleep(1000);
       system("cls");
   }
   
}

这串代码部分出自菜鸟教程

再看下一串

#include <iostream>
#include <ctime>
#include <windows.h>
#include <bits/stdc++.h>
using namespace std;
int main()
{
   for( ; ; )
   {
        time_t rawtime;
        struct tm *ptminfo;
        time(&rawtime);
        ptminfo = localtime(&rawtime);
        printf(": %02d-%02d-%02d %02d:%02d:%02d\n",
        ptminfo->tm_year + 1900, ptminfo->tm_mon + 1, ptminfo->tm_mday,  
        ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec); 
        Sleep(1000);
        system("cls");
   }
   
}

这串也有部分出自其他博主的但我不知道是谁有知道的可以在评论区说一下

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐