当前位置:首页 » 编程语言 » c语言判断时间
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言判断时间

发布时间: 2022-07-02 07:04:39

c语言中输入年月日,判断这一天是一年的第几天……

以2月10日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。

源代码:

#include "stdio.h"

#include "stdlib.h"

int main()
{
int day,month,year,sum,leap;

printf(" please input year,month,day ");

scanf("%d,%d,%d",&year,&month,&day);

switch(month) /*先计算某月以前月份的总天数*/
{
case 1:sum=0;break;

case 2:sum=31;break;

case 3:sum=59;break;

case 4:sum=90;break;

case 5:sum=120;break;

case 6:sum=151;break;

case 7:sum=181;break;

case 8:sum=212;break;

case 9:sum=243;break;

case 10:sum=273;break;

case 11:sum=304;break;

case 12:sum=334;break;
default:printf("data error ");break;
}
sum=sum+day; /*再加上某天的天数*/

if(year%400==0||(year%4==0&&year%100!=0)) /*判断是不是闰年*/
{
leap=1;
}
else
{
leap=0;
}

if(leap==1&&month>2) /*如果是闰年且月份大于2,总天数应该加一天*/
{
sum++;
}
printf("It is the %dth day. ",sum);
return 0;
}

输出

please input year,month,day

2019,02,10

It is the 41th day.

(1)c语言判断时间扩展阅读

c语言编写程序根据输入的时间分别输出问候语

#include <stdio.h>
int main()
{
int a;
printf("请输入时间,例如:17");

scanf("%d",&a);

if(a<=12&&a>=0) printf("早");

else if(a>12&&a<=14) printf("午");

else printf("晚");

printf("%d",s);

return 0;

❷ 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语言时间函数:
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语言的时间函数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为一个结构体,包含了年月日时分秒等信息。

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

二、参考代码:

#include<stdio.h>
#include<time.h>
intmain()
{
time_tt;
structtm*lt;
time(&t);//获取Unix时间戳。
lt=localtime(&t);//转为时间结构。
printf("%d/%d/%d%d:%d:%d ",lt->tm_year+1900,lt->tm_mon,lt->tm_mday,lt->tm_hour,lt->tm_min,lt->tm_sec);//输出结果
return0;
}

注意事项:

struct tm中的tm_year 值为实际年减去1900, 所以输出的时候要是lt->tm_year+1900。

❺ c语言编程,怎么计算时间

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

void main()

{

unsigned char time1[] = {10, 8, 31, 9, 26 };

unsigned char time2[] = { 10, 8, 31, 9, 50 };

struct tm t1 = {0};

struct tm t2 = {0};

time_t _t1;

time_t _t2;

double diff;

t1.tm_year = time1[0] + 100;

t1.tm_mon = time1[1];

t1.tm_mday = time1[2];

t1.tm_hour = time1[3];

t1.tm_min = time1[4];

t2.tm_year = time2[0] + 100;

t2.tm_mon = time2[1];

t2.tm_mday = time2[2];

t2.tm_hour = time2[3];

t2.tm_min = time2[4];

_t1 = _mkgmtime( &t1 );

_t2 = _mkgmtime( &t2 );

diff = difftime(_t2, _t1 );

printf( "相差 %.0f 分钟 ", diff / 60 );

}

(5)c语言判断时间扩展阅读:

C语言中有两个相关的函数用来计算时间差,分别是:

time_t time( time_t *t) 与 clock_t clock(void)

头文件: time.h

计算的时间单位分别为: s , ms

time_t 和 clock_t 是函数库time.h 中定义的用来保存时间的数据结构

返回值:

1、time : 返回从公元1970年1月1号的UTC时间从0时0分0秒算起到现在所经过的秒数。如果参数 t 非空指针的话,返回的时间会保存在 t 所指向的内存。

2、clock:返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数。 1单元 = 1 ms。

所以我们可以根据具体情况需求,判断采用哪一个函数。

具体用法如下例子:

#include <time.h>

#include <stdio.h>

#include <stdlib.h>

int main()

{

time_t c_start, t_start, c_end, t_end;

c_start = clock(); //!< 单位为ms

t_start = time(NULL); //!< 单位为s

system("pause");

c_end = clock();

t_end = time(NULL);

//!<difftime(time_t, time_t)返回两个time_t变量间的时间间隔,即时间差

printf("The pause used %f ms by clock() ",difftime(c_end,c_start));

printf("The pause used %f s by time() ",difftime(t_end,t_start));

system("pause");

return 0;

}

因此,要计算某一函数块的占用时间时,只需要在执行该函数块之前和执行完该函数块之后调用同一个时间计算函数。再调用函数difftime()计算两者的差,即可得到耗费时间。

❻ c语言怎么判定时间01点到24点是那个时间段中

int hour; //当前时间
if ( (hour>=18) && (hour<22) )
printf("当天时间位于1与2之间\n");
else if ( (hour>=22)|| (hour<2) )
printf("当天时间位于2与3之间\n");
else if ( (hour>=2) && (hour<5) )
printf("当天时间位于3与4之间\n");
else if ( (hour>=5) && (hour<8) )
printf("当天时间位于4与5之间\n");
else if ( (hour>=8) && (hour<18) )
printf("当天时间位于5与1之间\n");

❼ C语言 判断本机时间是上午还是下午

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{
tm* pt;
time_t t=time(0);
pt=localtime(&t);
if(pt->tm_hour<12)
printf("现在是上午:%d:%d\n",pt->tm_hour,pt->tm_min);
else
printf("现在是下午:%d:%d\n",pt->tm_hour,pt->tm_min);
}

❽ c语言 输入一个时间(年、月、日、时、分、秒),判断时间是否合法,输出下一秒的时间

#include<stdio.h>

int year = 0;

int month = 0;

int day = 0;

int hour = 0;

int minute = 0;

int second = 0;


int main()

{

void inputDate(); /*输入年-月-日 时:分:秒*/

void nextSceond(); /*计算下一秒的时间*/

int leapYear(int year); /*判断是否为闰年*/

int dayMonth(int month); /*返回每个月份对应的天数*/

inputDate();

leapYear(year);

dayMonth(month);

nextSceond();

system("PAUSE");

return 0;

}


/*函数inputDate()输入年-月-日 时:分:秒*/

void inputDate()

{

int loop;

for(loop = 0; loop < 3; loop++)

{

printf("请输入年-月-日 时:分:秒:");

scanf("%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);

if(month < 1 || month > 12)

{

printf(" 月份输入错误! ");

continue;

}

else if(day < 1 || day > dayMonth(month))

{

printf(" 日期输入错误! ");

continue;

}

else if(hour < 0 || hour > 23)

{

printf(" 小时输入错误! ");

continue;

}

else if(minute < 0 || minute > 59)

{

printf(" 分钟输入错误! ");

continue;

}

else if(second < 0 || second > 59)

{

printf(" 秒数输入错误! ");

continue;

}

else

{

break;

}

}

}



/*函数nextSecond()计算下一秒的时间*/

void nextSceond()

{

if(59 == second)

{

minute += 1;

second = 0;

if(60 == minute)

{

hour += 1;

minute = 0;

if(24 == hour)

{

day += 1;

hour = 0;

if(day > dayMonth(month))

{

month += 1;

day = 1;

if(13 == month)

{

year += 1;

month = 1;

}

}

}

}

}

else

{

second += 1;

}

printf("%d-%d-%d %d:%d:%d ",year, month, day, hour, minute, second);

}



/*函数leapYear(int year)判断是否为闰年*/

int leapYear(int year)

{

if(0 == (year % 4 && 0 != year % 100) || 0 == year % 400)

{

return 1;

}

else

{

return 0;

}

}


/*函数名dayMonth(int month)返回每个月份对应的天数*/

int dayMonth(int month)

{

switch(month)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

return 31;

case 2:

if(0 == (year % 4 && 0 != year % 100) || 0 == year %400)

{

return 29;

}

else

{

return 28;

}

case 4:

case 6:

case 9:

case 11:

return 30;

}

}

  • 请放心使用

  • 有问题的话请追问

  • 满意请及时采纳,谢谢

❾ C语言怎么判断时间

本人(无界)用C-Free编写,亲测可用

#include<stdio.h>
#include<time.h>

intmain()
{
time_tt_ntime;//t_ntime用于储存当前时间
structtmsttm_time;//声明tm结构体sttm_time。tm结构体定义在头文件time.h中
//以下给结构体sttm_time赋值,2011-1-1时的数值查的如下
sttm_time.tm_sec=0;
sttm_time.tm_min=0;
sttm_time.tm_hour=0;
sttm_time.tm_mday=2; //因为要超过2011-1-1,所以判断的依据是到第二天2011-1-2
sttm_time.tm_mon=0;
sttm_time.tm_year=111;
sttm_time.tm_wday=0;
sttm_time.tm_yday=1;
sttm_time.tm_isdst=-1;

t_ntime=time(NULL);//获取当前本地时间
if(t_ntime>=mktime(&sttm_time))printf("当前本地时间已经超过2011-1-1 ");//判断
elseprintf("当前本地时间未超过2011-1-1 ");
return(0);
}
---“无界”回答