C++中获取当前时间的代码
答案:4 悬赏:60 手机版
解决时间 2021-04-10 22:32
- 提问者网友:轻浮
- 2021-04-10 16:25
最好分别获取当前的年月日时分秒 分别放到变量里 想做一个闹钟程序
最佳答案
- 五星知识达人网友:夜风逐马
- 2021-04-10 17:52
#include <iostream.h>
#include <ctime>
void main()
{
tm *year; //年
tm *month; //月
tm *day; //日
tm *hour;//时
tm *minute;
tm *second;
time_t t;
t=time(0);
year = localtime(&t);
month = localtime(&t);
day = localtime(&t);
hour = localtime(&t);
minute = localtime(&t);
second = localtime(&t);
cout<<(year ->tm_year+1900)<<"年" //返回的是今年减1900,所以要加上1900
<<(month->tm_mon+1)<<"月" //因为Month (0 – 11; January = 0),所以要加1
<<day ->tm_mday<<"日"
<<hour ->tm_hour<<"时"
<<minute->tm_min<<"分"
<<second->tm_sec<<"秒"
<<endl;
}
全部回答
- 1楼网友:逐風
- 2021-04-10 20:41
#include <afxwin.h>
#include <windows.h>
#include <stdio.h>
void main()
{
CTime t;
int year,month,day,hour,minute,second;
while(1)
{
t=CTime::GetCurrentTime();
year=t.GetYear();
month=t.GetMonth();
day=t.GetDay();
hour=t.GetHour();
minute=t.GetMinute();
second=t.GetSecond();
CString temp;
temp.Format(_T("现在是%d年%d月%d日%d时%d分%d秒"),year,month,day,hour,minute,second);
//AfxMessageBox(temp);
printf("%s\n",temp);
Sleep(1000);
system("cls");
}
system("pause>nul");
}
- 2楼网友:独钓一江月
- 2021-04-10 19:28
可以用CTime这个类 CTime t = CTime::GetCurrentTime(); 在用类里面的成员函数获取所需要的时间信息
- 3楼网友:廢物販賣機
- 2021-04-10 18:23
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯