本文目錄一覽:
- 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語言比較當前系統日期與我輸入的日期大小怎麼寫代碼?
#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(“%d\n”,p-tm_sec); /*獲取當前秒*/
// printf(“%d\n”,p-tm_min); /*獲取當前分*/
// printf(“%d\n”,8+p-tm_hour);/*獲取當前時,這裡獲取西方的時間,剛好相差八個小時*/
// printf(“%d\n”,p-tm_mday);/*獲取當前月份日數,範圍是1-31*/
// printf(“%d\n”,1+p-tm_mon);/*獲取當前月份,範圍是0-11,所以要加1*/
// printf(“%d\n”,1900+p-tm_year);/*獲取當前年份,從1900開始,所以要加1900*/
// printf(“%d\n”,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
%s\n”,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=%d\n”,max);
system(“pause”);
return
0;
}
//—————————————————————————
原創文章,作者:VJOD,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/141214.html