资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言系统日期函数 c语言 日期

在c语言中如何使用系统函数得到当前的日期?

获得日期和时间

创新互联公司是一家专注于成都做网站、成都网站设计与策划设计,谢通门网站建设哪家好?创新互联公司做网站,专注于网站建设10年,网设计领域的专业建站公司;建站业务涵盖:谢通门等地区。谢通门做网站价格咨询:13518219792

这里说的日期和时间就是我们平时所说的年、月、日、时、分、秒等信息。从第2节我们已经知道这些信息都保存在一个名为tm的结构体中,那么如何将一个日历时间保存为一个tm结构的对象呢?

其中可以使用的函数是gmtime()和localtime(),这两个函数的原型为:

struct

tm

*

gmtime(const

time_t

*timer);

struct

tm

*

localtime(const

time_t

*

timer);

其中gmtime()函数是将日历时间转化为世界标准时间(即格林尼治时间),并返回一个tm结构体来保存这个时间,而localtime()函数

是将日历时间转化为本地时间。比如现在用gmtime()函数获得的世界标准时间是2005年7月30日7点18分20秒,那么我用

localtime()函数在中国地区获得的本地时间会比世界标准时间晚8个小时,即2005年7月30日15点18分20秒。下面是个例子:

#include

"time.h"

#include

"stdio.h"

int

main(void)

{

struct

tm

*local;

time_t

t;

t=time(NUL);

local=localtime(t);

printf("Local

hour

is:

%d\n",local-tm_hour);

local=gmtime(t);

printf("UTC

hour

is:

%d\n",local-tm_hour);

return

0;

}

运行结果是:

Local

hour

is:

15

UTC

hour

is:

7

固定的时间格式

我们可以通过asctime()函数和ctime()函数将时间以固定的格式显示出来,两者的返回值都是char*型的字符串。返回的时间格式为:

星期几

月份

日期

时:分:秒

年\n{post.content}

例如:Wed

Jan

02

02:03:55

1980\n{post.content}

其中\n是一个换行符,{post.content}是一个空字符,表示字符串结束。下面是两个函数的原型:

Char

*

asctime(const

struct

tm

*

timeptr);

char

*

ctime(const

time_t

*timer);

其中asctime()函数是通过tm结构来生成具有固定格式的保存时间信息的字符串,而ctime()是通过日历时间来生成时间字符串。这样的

话,asctime()函数只是把tm结构对象中的各个域填到时间字符串的相应位置就行了,而ctime()函数需要先参照本地的时间设置,把日历时间转

化为本地时间,然后再生成格式化后的字符串。在下面,如果t是一个非空的time_t变量的话,那么:

printf(ctime(t));

等价于:

struct

tm

*ptr;

ptr=localtime(t);

printf(asctime(ptr));

那么,下面这个程序的两条printf语句输出的结果就是不同的了(除非你将本地时区设为世界标准时间所在的时区):

#include

"time.h"

#include

"stdio.h"

int

main(void)

{

struct

tm

*ptr;

time_t

lt;

lt

=time(NUL);

ptr=gmtime();

printf(asctime(ptr));

printf(ctime());

return

0;

}

运行结果:

Sat

Jul

30

08:43:03

2005

Sat

Jul

30

16:43:03

2005

C语言日期函数设计

#includestdio.h

#includestdlib.h

#includeconio.h

#includetime.h

int days(int year,int month,int day)

{

if(month==1||month==2)//判断month是否为1或2 

{

year--;

month+=12;

}

int c=year/100;

int y=year-c*100;

int week=(c/4)-2*c+(y+y/4)+(13*(month+1)/5)+day-1;

while(week0)week+=7;

week%=7;

return week;

}

int years(int year)

{

if((year%4==0year%100!=0)||(year%400==0))return 1;

else return 0;

}

int months(int year,int month)

{

switch(month)

{

case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31;

case 4:case 6:case 9:case 11:return 30;

case 2:if(years(year))return 29;

else return 28;

default:return -1;

}

}

void print(int year,int month)

{

system("cls");

if(year0)printf("公元前");

struct tm *p; //时间结构体

time_t t; //把当前时间给t

t=time(NULL); //NULL为0

p=localtime(t);//获取当前系统时间

printf("%d年%d月\n(现在是北京时间%d年%d月%d日周",abs(year),month, 1900+p-tm_year, 1+p-tm_mon, p-tm_mday, p-tm_hour, p-tm_min, p-tm_sec);

if(p-tm_wday==1)printf("一");

else if(p-tm_wday==2)printf("二");

else if(p-tm_wday==3)printf("三");

else if(p-tm_wday==4)printf("四");

else if(p-tm_wday==5)printf("五");

else if(p-tm_wday==6)printf("六");

else if(p-tm_wday==7)printf("日");

printf("%d点%d分%d秒)\n", p-tm_hour, p-tm_min, p-tm_sec);

printf("周日 周一 周二 周三 周四 周五 周六\n");

int i,add=1,place=days(year,month,1);

for(i=1;i=place*5;i++)printf(" ");

int place2=7-place;

for(i=1;i=place2;i++)

{printf("%4d ",add);

add++;

}

printf("\n");

while(add=months(year,month))

{

printf("%4d",add);

add++;

if((add-place2)%7==1)printf("\n");

else printf(" ");

}

printf("\n—\t--:上月 --:下月 \t—\n— Esc:退出 空格:设置年份 —\n");

}

main()

{

struct tm *local; //时间结构体

time_t t; //把当前时间给t

t=time(NULL); //NULL为0

local=localtime(t);//获取当前系统时间

int year=1900+local-tm_year,month=1+local-tm_mon;

char input=0;

print(year,month);

while(1)

{

input=getch();

if(input==27)

{system("cls");

printf("\n\n\t谢谢使用\n 请按任意键继续...\n");

getch();

exit(0);

}

else if(input==75)

{month--;

if(month=0)

{month=12;if(year(-999))year--;if(year==0)year--;}

}

else if(input==77)

{

month++;

if(month=13)

{month=1;year++;if(year==0)year++;}

}

else if(input==' ')

{

while(1)

{ system("cls");

printf("┌────┐\n");

printf("│SetYear │\n");

printf("│%4d 年 │\n",year);

printf("-,-:switch\n");

printf("Enter:choose\n");

printf("└────┘\n");

switch(getch())

{

case 27:system("cls");

printf("\n\n\t谢谢使用\n 请按任意键继续...\n");

getch();

exit(0);

case 75:if (year(-999))year--;if(year==0)year--;break;

case 77:year++;if(year==0)year++;break;

case 13:goto end;

}

}

}end:

print(year,month);

}

}

我给你一个我刚编的程序(日历),供您参考

c语言 时间函数

c语言时间函数:

1、获得日历时间函数:

可以通过time()函数来获得日历时间(Calendar Time),其原型为:time_t time(time_t * timer);

如果已经声明了参数timer,可以从参数timer返回现在的日历时间,同时也可以通过返回值返回现在的日历时间,即从一个时间点(例如:1970年1月1日0时0分0秒)到现在此时的秒数。如果参数为空(NUL),函数将只通过返回值返回现在的日历时间,比如下面这个例子用来显示当前的日历时间:

2、获得日期和时间函数:

这里说的日期和时间就是平时所说的年、月、日、时、分、秒等信息。从第2节我们已经知道这些信息都保存在一个名为tm的结构体中,那么如何将一个日历时间保存为一个tm结构的对象呢?

其中可以使用的函数是gmtime()和localtime(),这两个函数的原型为:

struct tm * gmtime(const time_t *timer);

struct tm * localtime(const time_t * timer);

其中gmtime()函数是将日历时间转化为世界标准时间(即格林尼治时间),并返回一个tm结构体来保存这个时间,而localtime()函数是将日历时间转化为本地时间。比如现在用gmtime()函数获得的世界标准时间是2005年7月30日7点18分20秒,那么用localtime()函数在中国地区获得的本地时间会比世界标准时间晚8个小时,即2005年7月30日15点18分20秒。

用c语言如何获取系统当前时间的函数?

1、C语言中读取系统时间的函数为time(),其函数原型为:\x0d\x0a#include \x0d\x0atime_t time( time_t * ) ;\x0d\x0atime_t就是long,函数返回从1970年1月1日(MFC是1899年12月31日)0时0分0秒,到现在的的秒数。\x0d\x0a2、C语言还提供了将秒数转换成相应的时间格式的函数:\x0d\x0a char * ctime(const time_t *timer); //将日历时间转换成本地时间,返回转换后的字符串指针 可定义字符串或是字符指针来接收返回值\x0d\x0a struct tm * gmtime(const time_t *timer); //将日历时间转化为世界标准时间(即格林尼治时间),返回结构体指针 可定义struct tm *变量来接收结果\x0d\x0a struct tm * localtime(const time_t * timer); //将日历时间转化为本地时间,返回结构体指针 可定义struct tm *变量来接收结果\x0d\x0a3、例程:\x0d\x0a#include \x0d\x0avoid main()\x0d\x0a{\x0d\x0a time_t t;\x0d\x0a struct tm *pt ;\x0d\x0a char *pc ;\x0d\x0a time(t);\x0d\x0a pc=ctime(t) ; printf("ctime:%s", pc );\x0d\x0a pt=localtime(t) ; printf("year=%d", pt-tm_year+1900 );\x0d\x0a}\x0d\x0a\x0d\x0a时间结构体struct tm 说明:\x0d\x0a\x0d\x0astruct tm { \x0d\x0a int tm_sec; /* 秒 _ 取值区间为[0,59] */ \x0d\x0a int tm_min; /* 分 - 取值区间为[0,59] */ \x0d\x0a int tm_hour; /* 时 - 取值区间为[0,23] */ \x0d\x0a int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */ \x0d\x0a int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */ \x0d\x0a int tm_year; /* 年份,其值等于实际年份减去1900 */ \x0d\x0a int tm_wday; /* 星期 _ 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */ \x0d\x0a int tm_yday; /* 从每年的1月1日开始的天数 _ 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */ \x0d\x0a int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/ \x0d\x0a};

C语言的时间函数

C语言的建时间函数是 mktime(),原型在 time.h 里

调用有点繁。

下面,用我的程序输入 年月日时分秒,调用mktime(), 就得 C语言 可直接使用的 时间, 存放在 t 里。

例如 输入年月日时分秒: 2008 8 16 9 55 25

time_t t; 里 就有了 各种时间信息,例如星期几...

#include stdio.h

#include time.h

void main(){

struct tm *target_time;

time_t rawtime, t;

int year,month,mday,hh,mm,ss;

time ( rawtime );

target_time = localtime ( rawtime );

printf("Please enter year month day hour minute second\n");

printf("For example: \n");

printf("2008 8 16 9 55 25\n");

scanf("%d %d %d %d %d %d", year, month, mday, hh,mm,ss);

target_time-tm_year = year - 1900;

target_time-tm_mon= month - 1;

target_time-tm_mday = mday ;

target_time-tm_hour = hh ;

target_time-tm_min = mm ;

target_time-tm_sec = ss ;

//

t = mktime (target_time);

// t is ready to use

printf("%s ",ctime(t));

}

C语言获取系统时间

需要利用C语言的时间函数time和localtime,具体说明如下:

一、函数接口介绍:

1、time函数。

形式为time_t time (time_t *__timer);

其中time_t为time.h定义的结构体,一般为长整型。

这个函数会获取当前时间,并返回。 如果参数__timer非空,会存储相同值到__timer指向的内存中。

time函数返回的为unix时间戳,即从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。

由于是秒作为单位的,所以这并不是习惯上的时间,要转为习惯上的年月日时间形式就需要另外一个函数了。

2、localtime函数。

形式为struct tm *localtime (const time_t *__timer);

其中tm为一个结构体,包含了年月日时分秒等信息。

这种结构是适合用来输出的。


分享文章:c语言系统日期函数 c语言 日期
本文来源:http://cdkjz.cn/article/hpjegi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220