c++输出现在时间

本文将用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");
   }
   
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值