您的位置:首页 > 编程语言 > C语言/C++

c语言获取系统时间 程序实现

2014-04-20 09:35 375 查看

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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  系统时间 时区 time.h