C++實現(xiàn)當前時間動態(tài)顯示的方法
更新時間:2015年07月14日 11:33:53 作者:G0561
這篇文章主要介紹了C++實現(xiàn)當前時間動態(tài)顯示的方法,涉及C++時間操作及Sleep方法的使用,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了C++實現(xiàn)當前時間動態(tài)顯示的方法。分享給大家供大家參考。具體如下:
/* 24-06-10 10:44 動態(tài)顯示時間 但不是最優(yōu)的 功能很單一 本程序關(guān)鍵是對時鐘函數(shù)的使用 **tm結(jié)構(gòu)定義了 年、月、日、時、分、秒、星期、當年中的某一天、夏令時 **用localtime獲取當前系統(tǒng)時間,該函數(shù)將一個time_t時間轉(zhuǎn)換成tm結(jié)構(gòu)表示的時間,函數(shù)原型: struct tm * localtime(const time_t *) **使用gmtime函數(shù)獲取格林尼治時間,函數(shù)原型: struct tm * gmtime(const time_t *) 包含的頭文件是time.h */ //struct tm { // int tm_sec; /* seconds after the minute - [0,59] */ // int tm_min; /* minutes after the hour - [0,59] */ // int tm_hour; /* hours since midnight - [0,23] */ // int tm_mday; /* day of the month - [1,31] */ // int tm_mon; /* months since January - [0,11] */ // int tm_year; /* years since 1900 */ // int tm_wday; /* days since Sunday - [0,6] */ // int tm_yday; /* days since January 1 - [0,365] */ // int tm_isdst; /* daylight savings time flag */ // }; #include <iostream> #include <time.h> #include "dos.h" #include <windows.h> using namespace std; int main() { char *myweek[]={"日","一","二","三","四","五","六"}; time_t nowtime; //typedef long time_t;在編譯器定義的頭文件中 nowtime = time(NULL); //獲取當前時間 此時它是用一個長整形表示的 struct tm *local; /*時間結(jié)構(gòu)體變量*/ local = localtime(&nowtime); //獲取當前系統(tǒng)時鐘 while (1) { cout<<"當前時間:"; cout<<local->tm_year+1900<<"年"<<local->tm_mon+1<<"月"<<local->tm_mday<<"日"<<" "; cout<<local->tm_hour<<"時"<<local->tm_min<<"分"<<local->tm_sec<<"秒"<<" "; cout<<"星期"<<myweek[local->tm_wday]<<endl; /* 對當前時間進行判斷 讓它動態(tài)變化 */ if(local->tm_sec==59 && local->tm_min!=59) //當秒到59,分未到59時 分鐘加1,秒清0 { local->tm_min++; local->tm_sec=0; } //當秒和分都為59 時不為23時 ,秒和分鐘都清0,時鐘加1 else if(local->tm_sec==59 && local->tm_min==59 && local->tm_hour!=23) { local->tm_min=0; local->tm_sec=0; local->tm_hour++; } //當秒和分都為59 時為23時 ,秒,分鐘和時鐘都清0 else if(local->tm_sec==59&&local->tm_min==59&&local->tm_hour==23) { local->tm_sec=0; local->tm_min=0; local->tm_hour=0; } else //其它情況秒鐘進行不斷加1 { local->tm_sec++; } Sleep(1000); /*Sleep()里面的單位,是以毫秒為單位, system("cls"); /*清屏命令 出現(xiàn)動態(tài)顯示*/ } system("pause"); return 0; }
希望本文所述對大家的C++程序設(shè)計有所幫助。
您可能感興趣的文章:
- C++自定義函數(shù)判斷某年某月某日是這一年中第幾天
- C++實現(xiàn)兩個日期間差多少天的解決方法
- C++時間戳轉(zhuǎn)換成日期時間的步驟和示例代碼
- C++ boost 時間與日期處理詳細介紹
- C++實現(xiàn)日期類(Date類)的方法
- C++獲取當前系統(tǒng)時間的方法總結(jié)
- C++中獲取UTC時間精確到微秒的實現(xiàn)代碼
- C++設(shè)置系統(tǒng)時間及系統(tǒng)時間網(wǎng)絡(luò)更新的方法
- VC++ 獲取系統(tǒng)時間的方法匯總
- C++取得當前時間的方法
- C++基于蔡基姆拉爾森計算公式實現(xiàn)由年月日確定周幾的方法示例
相關(guān)文章
C++實現(xiàn)LeetCode(122.買股票的最佳時間之二)
這篇文章主要介紹了C++實現(xiàn)LeetCode(122.買股票的最佳時間之二),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07C++設(shè)置系統(tǒng)時間及系統(tǒng)時間網(wǎng)絡(luò)更新的方法
這篇文章主要介紹了C++設(shè)置系統(tǒng)時間及系統(tǒng)時間網(wǎng)絡(luò)更新的方法,涉及網(wǎng)絡(luò)程序設(shè)計與系統(tǒng)函數(shù)的使用,需要的朋友可以參考下2014-10-10Qt創(chuàng)建SQlite數(shù)據(jù)庫的示例代碼
本文主要介紹了Qt創(chuàng)建SQlite數(shù)據(jù)庫的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05淺析C++中memset,memcpy,strcpy的區(qū)別
本篇文章是對C++中memset,memcpy,strcpy的區(qū)別進行了詳細的分析介紹,需要的朋友參考下2013-07-07C++訪問Redis的mset 二進制數(shù)據(jù)接口封裝方案
這篇文章主要介紹了C++訪問Redis的mset 二進制數(shù)據(jù)接口封裝方案的相關(guān)資料,需要的朋友可以參考下2015-07-07