當前位置:首頁 » 編程語言 » 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);
}
---「無界」回答