VC++獲得當前系統(tǒng)時間的幾種方案
作者:flyingfish 
  在開發(fā)應用程序時往往需要獲取當前系統(tǒng)時間。盡管Y2K似乎已經(jīng)平安過去,但在我們新開發(fā)的應用程序中還是要謹慎處理“時間”問題。
 //方案- 優(yōu)點:僅使用C標準庫;缺點:只能精確到秒級
- #include < time.h>
 - #include < stdio.h>
 - int main( void )
 - {
 - time_t t = time( 0 );
 - char tmp[64];
 - strftime( tmp, sizeof(tmp), " %Y/%m/%d %X %A 本年第%j天 %z" , localtime(&t) );
 - puts( tmp );
 - return 0;
 - }
 
//方案二 優(yōu)點:能精確到毫秒級;缺點:使用了windows API
- #include < windows.h>
 - #include < stdio.h>
 - int main( void )
 - {
 - SYSTEMTIME sys;
 - GetLocalTime( &sys );
 - printf( " %4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d\n"
 - ,sys.wYear,sys.wMonth,sys.wDay
 - ,sys.wHour,sys.wMinute,sys.wSecond,sys.wMilliseconds
 - ,sys.wDayOfWeek);
 - return 0;
 - }
 
//方案三,優(yōu)點:利用系統(tǒng)函數(shù)
- #include< stdlib.h>
 - #include< iostream>
 - using namespace std;
 - void main(){
 - system(" time" );
 - }
 
可以改變電腦的時間設(shè)定
方案4:
- #include< iostream>
 - #include< ctime>
 - using namespace std;
 - int main()
 - {
 - time_t now_time;
 - now_time = time(NULL);
 - cout< < now_time;
 - return 0;
 - }
 
另一:_strdate(tempstr);
另二: CTime
- CString CTestView::GetTime()
 - {
 - CTime CurrentTime=CTime::GetCurrentTime();
 - CString strTime;
 - strTime.Format(" %d:%d:%d" ,CurrentTime.GetHour(), CurrentTime.GetMinute()
 - ,CurrentTime.GetSecond());
 - return strTime;
 - }
 
【編輯推薦】
責任編輯:于鐵 
                    來源:
                    flyingfish
 














 
 
 
 
 
 
 