宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

本文目录一览:

  • 1、C语言的比较两个时间的函数
  • 2、用c语言比较当前系统日期与我输入的日期大小怎么写代码?
  • 3、C语言下如何获得当前时间,并显示出来 然后比较两个时间的大小
  • 4、C语言如何比较时间的大小?
  • 5、C语言写的比较时间大小的程序
  • 6、C语言判断两个日期大小

C语言的比较两个时间的函数

1、方法一:若时间为结构体变量,比较两个时间的大小,而且不能改变时间的值,可以是:

int timecmp(date_t* date1,date_t* date2)

{

if(date1- year==date1- year)

return memcmp(date1, date2,sizeof(date_t));

else

return date1- year-date2- year

}

2、方法二:

long getTimeInterval(const char *t1, const char *t2) {

struct tm tm1, tm2;

time_t start, end;

double diff;

memset(tm1, 0, sizeof(tm1));

memset(tm2, 0, sizeof(tm2));

strptime(t1, “%Y%m%d”, tm1);

start = mktime(tm1);

strptime(t2, “%Y%m%d”, tm2);

end = mktime(tm2);

diff = difftime(start, end);

return d2l(diff);

}

调用:

printf(“getTimeInterval=[%ld]n”, getTimeInterval(“20101221”, “20110326”));

printf(“getTimeInterval=[%ld]n”, getTimeInterval(“20101221”, “20990326”));

第一行输出:[-8208000]

第二行输出:[1292860801]

3、补充: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语言如何比较时间大小,c语言 比大小(两个时间大小比较)-风君雪科技博客

用c语言比较当前系统日期与我输入的日期大小怎么写代码?

#include time.h

#include stdio.h

void main(void)

{

time_t timep;

struct tm *p;

int in_time[3];

int now_time[3];

int i;

printf(“输入年-月-日: “);

scanf(“%d-%d-%d”, in_time[0], in_time[1], in_time[2]);

time (timep);

p=gmtime(timep);

now_time[0]=1900+p-tm_year;

now_time[1]=1+p-tm_mon;

now_time[2]=p-tm_mday;

for(i=0;i3;i++)

if(in_time[i]now_time[i]) 

{

printf(“你输入的日期大n”);

break;

}

else if(in_time[i]now_time[i]) 

{

printf(“你输入的日期小n”);

break;

}

else 

continue;

if(i==3)

printf(“两个日期一样大n”);

// printf(“%dn”,p-tm_sec); /*获取当前秒*/

// printf(“%dn”,p-tm_min); /*获取当前分*/

// printf(“%dn”,8+p-tm_hour);/*获取当前时,这里获取西方的时间,刚好相差八个小时*/

// printf(“%dn”,p-tm_mday);/*获取当前月份日数,范围是1-31*/

// printf(“%dn”,1+p-tm_mon);/*获取当前月份,范围是0-11,所以要加1*/

// printf(“%dn”,1900+p-tm_year);/*获取当前年份,从1900开始,所以要加1900*/

// printf(“%dn”,p-tm_yday); /*从今年1月1日算起至今的天数,范围为0-365*/

}

C语言下如何获得当前时间,并显示出来 然后比较两个时间的大小

#include

//c语言的头文件

#include

//c语言的i/o

void

main()

{

time_t

now;

//实例化time_t结构

struct

tm

*timenow;

//实例化tm结构指针

time(now);

//time函数读取现在的时间(国际标准时间非北京时间),然后传值给now

timenow

=

localtime(now);

//localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区)

printf(“local

time

is

%sn”,asctime(timenow));

//上句中asctime函数把时间转换成字符,通过printf()函数输出

}

注释:time_t是一个在time.h中定义好的结构体。而tm结构体的原形如下:

struct

tm

{

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;

};

C语言如何比较时间的大小?

用字符串比较函数strcmp()

#includestdio.h

#include”string.h”

void main()

{

int n;

n=strcmp(“2010-04-30″,”2010-05-02”);//再根据n进行判断

printf(“%d”,n);

}

C语言写的比较时间大小的程序

#include stdio.h

main()

{

int h,m,h1,m1,h2,m2;

printf(“请输入时间1:”);

scanf(“%d,%d”,h1,m1);

printf(“请输入时间2:”);

scanf(“%d,%d”,h2,m2);

if (h1h2)

{

if (m1m2)

{

m=m1-m2;

h=h1-h2;

}

else

{

m=m1+60-m2;

h=h1-h2-1;

}

}

else

{

if (m1m2)

{

m=m2+60-m1;

h=h2-h1-1;

}

else

{

m=m2-m1;

h=h2-h1;

}

}

printf(“差额时间:%d:%d”,h,m);

}

C语言判断两个日期大小

程序没有问题,可以正确输出较大的数(特别注意一点:由于你的scanf()中的格式控制符是用逗号分隔的,所以在输入数字时也要用逗号分隔),至于你说的“稳定显示出来…执行完毕后按任意键继续”这个问题只需要包含头文件stdlib.h之后在main()结尾,return

0;之前加上system(“pause”);即可,原程序按此做如下修改:

//—————————————————————————

#include

#include

int

main(void)

{

int

x,y,max;

printf(“please

input

x,y=”);

scanf(“%d,%d”,x,y);

if(xy)

max=x;

else

max=y;

printf(“the

max

is=%dn”,max);

system(“pause”);

return

0;

}

//—————————————————————————