c语言时间加减函数 C语言将时间相加

c语言时间函数的具体使用方法,时间的加减

#include stdio.h

专注于为中小企业提供成都网站制作、成都网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业遂宁免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

#include time.h 

int main()

time_t rawtime; 

struct tm * timeinfo; 

time ( rawtime ); 

timeinfo = localtime ( rawtime ); 

printf ( "The current date/time is: %s", asctime (timeinfo) ); 

return 0;

}

说明:

time_t // 时间类型(time.h 定义) 

struct tm { // 时间结构,time.h 定义如下: 

int tm_sec; 

int tm_min; 

int tm_hour; 

int tm_mday; 

int tm_mon; 

int tm_year; 

int tm_wday; 

int tm_yday; 

int tm_isdst; 

time ( rawtime ); // 获取时间,以秒计,从1970年1月一日起算,存于rawtime 

localtime ( rawtime ); //转为当地时间,tm 时间结构 

asctime() // 转为标准ASCII时间格式: 

//就是直接打印tm,tm_year 从1900年计算,所以要加1900,月tm_mon,从0计算,所以要加1

c语言如何计算两个时间相差多少

/**

time.c

定义一个结构体实现两个时间的加减

*/

#includestdio.h

#includestring.h

typedef struct

{

int seconds;

int minutes;

int hours;

}Time;

int checkTime(Time time);

void printTime(Time time);

void swap(Time *time1,Time *time2);//大的时间放在前面

Time subtract1(Time *first,Time *second);

Time subtract(Time *first,Time *second);//默认第一个时间比第二个大

int main()

{

Time time1;

Time time2;

Time time3;

char againch[5]="y";

while(strcmp(againch,"y")==0||strcmp(againch,"Y")==0)

{

int again=1;

while(again)

{

printf("输入时间1:");

scanf("%d:%d:%d",time1.hours,time1.minutes,time1.seconds);

if(checkTime(time1))

{

printf("-----输入时间格式错误!请重新输入\n");

again=1;

}

else

again=0;

}

again=1;

while(again)

{

printf("输入时间2:");

scanf("%d:%d:%d",time2.hours,time2.minutes,time2.seconds);

if(checkTime(time2))

{

printf("-----输入时间格式错误!请重新输入\n");

again=1;

}

else

again=0;

}

swap(time1,time2);

printf("           ");

printTime(time1);

printf(" - ");

printTime(time2);

time3=subtract(time1,time2);

printf(" = ");

printTime(time3);

printf("\n");

printf("继续[y/n]?:");

scanf("%s",againch);

}

return 0;

}

//检查时间的格式

int checkTime(Time time)

{

//    printf("小时格式错误:%d\n",(time.hours=24||time.hours0));

//    printf("分钟格式错误:%d\n",(time.minutes=60||time.minutes0));

//    printf("秒格式错误  :%d\n",(time.seconds=60||time.minutes0));

return ((time.hours24||time.hours0)||(time.minutes=60||time.minutes0)||(time.seconds=60||time.minutes0));

}

//输出按个数输出时间

void printTime(Time time)

{

printf("%d:%d:%d",time.hours,time.minutes,time.seconds);

}

//大的时间放到第一个变量,小的时间方法哦第二个变量

void swap(Time *time1,Time *time2)

{

//保证第一个时间永远大于第二个时间

if(time2-hourstime1-hours)//如果有time

{

//交换两个时间的小时

time2-hours^=time1-hours;

time1-hours^=time2-hours;

time2-hours^=time1-hours;

//交换两个时间的分钟:

time1-minutes^=time2-minutes;

time2-minutes^=time1-minutes;

time1-minutes^=time2-minutes;

//交换两个时间的秒:

time1-seconds^=time2-seconds;

time2-seconds^=time1-seconds;

time1-seconds^=time2-seconds;

}

else if(time2-minutestime1-minutestime1-hours==time2-hours)

{

//交换两个时间的分钟:

time1-minutes^=time2-minutes;

time2-minutes^=time1-minutes;

time1-minutes^=time2-minutes;

//交换两个时间的秒:

time1-seconds^=time2-seconds;

time2-seconds^=time1-seconds;

time1-seconds^=time2-seconds;

}

else if(time2-secondstime1-secondstime1-minutes==time2-minutes)

{

//交换两个时间的秒:

time1-seconds^=time2-seconds;

time2-seconds^=time1-seconds;

time1-seconds^=time2-seconds;

}

}

//计算两个时间的差

Time subtract(Time *first,Time *second)//默认第一个时间比第二个大

{

Time result;

//先对秒进行相减

if(first-seconds=second-seconds)//如果第一个秒大于或者等于

{

result.seconds=first-seconds-second-seconds;

}

else//如果第一个的秒数小的话

{

first-minutes=first-minutes-1;//借位

first-seconds=first-seconds+60;

result.seconds=first-seconds-second-seconds;

}

//接着对分钟相减

if(first-minutes=second-minutes)//如果第一个秒大于或者等于

{

result.minutes=first-minutes-second-minutes;

}

else//如果第一个的秒数小的话

{

first-hours=first-hours-1;//借位

first-minutes=first-minutes+60;

result.minutes=first-minutes-second-minutes;

}

//交换后 默认第一个小时会大于第一个,没有借位的情况,不用

result.hours=first-hours-second-hours;

return result;

拓展资料

C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。

二十世纪八十年代,为了避免各开发厂商用的C语言语法产生差异,由美国国家标准局为C语言制定了一套完整的美国国家标准语法,称为ANSI C,作为C语言最初的标准。目前2011年12月8日,国际标准化组织(ISO)和国际电工委员会(IEC)发布的C11标准是C语言的第三个官方标准,也是C语言的最新标准,该标准更好的支持了汉字函数名和汉字标识符,一定程度上实现了汉字编程。

时间加减 C语言

在VC中我是这样弄的:

SYSTEMTIME time;

::GetSystemTime(time);\\取当前时间,time为一个临时的变量

CTime now=time;\\将当前时间存到CTime变量中

\\输入要进行对比的时间存入time中,比如下面(对比时间是2006年8月20日):

time.wYear=2006;

time.wMonth=8;

time.wDay=20;

CTime cmptime=time;\\将要对比的时间放到另一个CTime变量中

CTimeSpan overtime=now-cmptime;\\比较时,两时间直接作差,保存在CTimeSpan变量中,这个值有正负,overtime虽然是CTimeSpan类型的,但是用法和CTime一样

int overdays=overtime.GetDays();\\取天数差距,本例中就为9(今天是8月29日),如果刚才now-cmptime换成cmptime-now,现在的值就是-9

int overdays=overtime.GetMonths();同上,取月差距,其他方法一样,年,分,秒都能比较

自己回去试一试吧,应该有满意的效果!

我也是琢磨了很长时间才弄出来,个人觉得算是比较方便的方法了,正如你所说,时间的计算比较复杂,容易出错,不如让提供好的MFC类对时间进行处理,省去不必要的麻烦!


分享文章:c语言时间加减函数 C语言将时间相加
文章URL:http://myzitong.com/article/hhdpcc.html