c語言獲取系統時間 程序實現



1、

#include"stdafx.h"
#include<stdio.h>
#include<time.h>
#include<Windows.h>
int main(void)
{
 time_t timer=time(NULL);
 printf("ctime is%s\n",ctime(&timer));//得到日曆時間
 system("pause");
 return 0;
}

輸出爲:

ctime isSun Apr 20 09:07:27 2014

請按任意鍵繼續. . .


2、


#include"stdafx.h"
#include<time.h>
#include<stdio.h>
#include<dos.h>
#include<Windows.h>
int main()
{
 time_t timer;
 struct tm*tblock;
 timer=time(NULL);
 tblock=localtime(&timer);
 printf("Localtimeis:%s",asctime(tblock));
 system("pause");
return 0;
}

輸出爲:

Localtimeis:Sun Apr 20 09:10:00 2014
請按任意鍵繼續. . .


3、


#include"stdafx.h"
#include<Windows.h>
#include<stdio.h>
#include<time.h>
int main()
{
time_t t;
time(&t);
printf("Today's date and time:%s",ctime(&t));
system("pause");
return 0;
}


輸出爲:

Today's date and time:Sun Apr 20 09:13:23 2014
請按任意鍵繼續. . .

4、


#include"stdafx.h"
#include<Windows.h>
#include<time.h>
#include<stdio.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
 time_t first,second;
 //system("cls");//clrscr()是TC裏的
 first=time(NULL);
 Sleep(2000);//延遲函數,delay()是TC的
 second=time(NULL);
 printf("The difference is:%f seconds",difftime(second,first));//得到兩次機器時間差,單位爲秒
 system("pause");
 return 0;
}

輸出爲:

The difference is:2.000000 seconds請按任意鍵繼續. . .

5、


#include"stdafx.h"
#include<Windows.h>

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>
#include"stdafx.h"
#include<Windows.h>

char*tzstr="TZ=PST8PDT";//數字來改變時區,8代表
int main()
{
 time_t t;
 struct tm*gmt,*area;
 putenv(tzstr);//把字符串加到當前環境中
 tzset();//UNIX兼容函數,用於得到時區,在DOS環境下無用途
 t=time(NULL);
 area=localtime(&t);
 printf("Localtime is:%s",asctime(area));
 gmt=gmtime(&t);//得到以結構tm表示的時間信息
 printf("GMT is:%s",asctime(gmt));
 system("pause");
 return 0;
}


輸出爲:

Localtime is:Sat Apr 19 18:34:40 2014
GMT is:Sun Apr 20 01:34:40 2014
請按任意鍵繼續. . .


http://baike.baidu.com/view/1741851.htm


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章