當前位置:首頁 » 編程語言 » c語言日期轉換

c語言日期轉換

發布時間: 2022-06-11 15:59:55

c語言顯示日期日歷轉換代碼!!急急!!!!!!!

基本上就是這個思想,不太對,你再調試一下吧

#include <stdio.h>

#define YEAR 1970
typedef struct
{
int year;
int month;
int day;
int hour;
int min;
int sec;
} date;

/*儲存12個月的天數*/
const int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};

/*判斷是否為閏年*/
int isLeapYear(int year)
{
if(((year%4==0)&&(year%100!=0))
||(year%400==0))
{
return 1;
}
return 0;
}

/*日期轉成1970年1月1日起的天數*/
long date2sec(date d)
{
long sum=0;
int i;

//累計以往各年的天數
for(i=YEAR;i<d.year;i++)
{
sum+=365;
if(isLeapYear(i))
{//閏年多一天
sum+=1;
}
}
//累計當年以往各月的天數
for(i=0;i<d.month;i++)
{
sum+=days[i];
}
if(d.month>2)
{
if(isLeapYear(i))
{//閏年多一天
sum+=1;
}
}
//累計當年當月的天數
sum+=d.day-1;
//轉換成秒
sum=sum*24*60*60;

//加當天的小時,分鍾,秒
sum+=d.hour*60*60+d.min*60+d.sec;
//返回總秒數
return sum;
}

/*日期轉成1970年1月1日起的天數*/
date sec2date(long sec)
{
date d={YEAR,1,1,0,0,0};
int ds,sum;

ds=sec/(24*60*60);
sec-=ds*(24*60*60);
d.hour=sec/(60*60);
sec-=d.hour*(60*60);
d.min=sec/60;
sec-=d.min*60;
d.sec=sec;
//計算有多少年
while(1)
{
sum=365;
if(isLeapYear(d.year))
{//閏年多一天
sum+=1;
}
if(ds<sum)
{//不足一年了
break;
}
d.year++;
ds-=sum;
}

//計算有多少月
while(1)
{
sum=days[d.month-1];
if(d.month==2)
{
if(isLeapYear(d.year))
{//閏年多一天
sum+=1;
}
}
if(ds+1<sum)
{//不足一月了
d.day+=ds;
break;
}
d.month++;
ds-=sum;
}
return d;
}

main()
{
long sec;
date d;

puts("請輸入日期(格式為2008-08-08 08:08:08):");
scanf("%d-%d-%d %d:%d:%d",&d.year,&d.month,&d.day,&d.hour,&d.min,&d.sec);
sec=date2sec(d);
printf("到格林時間%d秒!\n",sec);
puts("請到格林時間的秒數:");
scanf("%d",&sec);
d=sec2date(sec);
printf("日期為:%d-%d-%d %d:%d:%d\n",d.year,d.month,d.day,d.hour,d.min,d.sec);
}

② C語言求助:時間格式轉換

#include
<stdio.h>
int
main()
{
int
h,m,s;
while(scanf("%d:%d:%d",&h,&m,&s))
{
if(h!=12)
{
h=h%12;
}
printf("PM:%02d:%02d:%02d\n",h,m,s);
}
}
未通過,是因為但h,m,s是個位數時,比如我輸入12:00:00,輸出的是AM:12:0:0,而不是AM:12:00:00,怎麼辦有什麼辦法?
對你這個問題
格式化輸出就可以了...
看我的代碼...
符合你的要求...

③ C語言中 怎樣把日期轉換成字元串

用代碼講解一下:
#include <stdio.h>
#include <time.h>
int main(void)
{
char buffer[128];
struct tm *datetime;
time_t current_time;
tzset();
time(¤t_time);
datetime = localtime(¤t_time);
strftime(buffer, sizeof(buffer), "%x %X", datetime);
printf("Using %%x %%X: %s\n", buffer);
strftime(buffer, sizeof(buffer), "%A %B %m, %Y", datetime);
printf("Using %%A %%B %%m %%Y: %s\n", buffer);
strftime(buffer, sizeof(buffer), "%I:%M%p", datetime);
printf("Using %%I:%%M%%p: %s\n", buffer);
return 1;
}

輸出結果:
Using %x %X: 09/09/12 18:10:33
Using %A %B %m %Y: Sunday September 09, 2012
Using %I:%M%p: 06:10PM

④ 用C語言編寫一個時間換算

剛才給的那個代碼太繁瑣了,又重新調整了一下:
#include <stdio.h>
int main()
{
//世界協調時與北京時間換算
int a;
scanf("%d",&a); /*輸入北京時間*/
if (a >= 800)
{
printf("%d\n",a-800); /*如果輸入的北京時間大於800,直接減去800,就是世界協調時*/
}
else{

int b=a/100; /*換算百位數字*/
int c=a%100; /*換算十位和個位數字*/
printf("%d\n",(24-(8-b))*100+c);
}

return 0;
}

⑤ C語言時間,怎麼把time_t類型的時間,轉化成年、月、日、時、分、秒呢

可以使用gmtime函數或localtime函數將time_t類型的時間日期轉換為struct tm類型(年、月、日、時、分、秒)。
使用time函數返回的是一個long值,該值對用戶的意義不大,一般不能根據其值確定具體的年、月、日等數據。gmtime函數可以方便的對time_t類型數據進行轉換,將其轉換為tm結構的數據方便數據閱讀。gmtime函數的原型如下:struct tm *gmtime(time_t *timep);localtime函數的原型如下:struct tm *localtime(time_t *timep);將參數timep所指的time_t類型信息轉換成實際所使用的時間日期表示方法,將結果返回到結構tm結構類型的變數。gmtime函數用來存放實際日期時間的結構變數是靜態分配的,每次調用gmtime函數都將重寫該結構變數。如果希望保存結構變數中的內容,必須將其復制到tm結構的另一個變數中。gmtime函數與localtime函數的區別:gmtime函數返回的時間日期未經時區轉換,是UTC時間(又稱為世界時間,即格林尼治時間)。localtime函數返回當前時區的時間。
轉換日期時間表示形式time_t類型轉換為struct tm類型示例:
#include <stdio.h>
#include <time.h>
int main()
{
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};/*指針字元數組*/ time_t t;
struct tm *p;
t=time(NULL);/*獲取從1970年1月1日零時到現在的秒數,保存到變數t中*/ p=gmtime(&t); /*變數t的值轉換為實際日期時間的表示格式*/
printf("%d年%02d月%02d日",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
printf(" %s ", wday[p->tm_wday]);
printf("%02d:%02d:%02d\n", p->tm_hour, p->tm_min, p->tm_sec);
return 0;
}
注意:p=gmtime(&t);此行若改為p=localtime(&t);則返回當前時區的時間。

⑥ c語言將日期轉換為字元串 急求,謝謝!

#include"stdio.h"

int main()

{ int y,m,d,c,w;

char week[7][10]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};

scanf("%d%d%d",&y,&m,&d);

if(m<3){m+=12;y--;}

c=y/100;

y%=100;

w=(y+y/4+c/4-2*c+26*(m+1)/10+d-1)%7;

printf("%d%d/%d/%d %s ",c,y,m,d,week[w]);

return 0;

}

⑦ C語言實驗--轉換日期格式

#include<stdio.h>

intmain(void)
{
inty,m,d;
scanf("%d/%d/%d",&m,&d,&y);
printf("%d.%02d.%02d",y,m,d);
return0;
}

⑧ C語言轉換日期的表示形式

這個可以嗎?
#include
void
main()
{
char
num[8];//定義一個字元數組num
int
i;//定義整型變數
printf("請輸入正確的日期!格式為:xxxxxxxx\n");
for(i=0;i<=8;i++)//循環語句,讀入8個字元,依次放到數組num中,作為8個元素
num[i]=getchar();
for(i=0;i<4;i++)//輸出前四個字元(年份)
putchar(num[i]);
putchar('_');
for(;i<6;i++)//輸出(月份)
putchar(num[i]);
putchar('_');
for(;i<8;i++)//輸出(日)
putchar(num[i]);
putchar('\n');//回車
}

⑨ c語言,如何進行日期格式轉換

time.h 有函數 strftime 輸出各種格式,但沒有 你的 11th 13rd 格式。
簡單辦法是用查表法

#include "stdio.h"
#include "stdlib.h"
void main()
{
char dmy[20]="13/12/2010";
int i,j;
int a,b,c;
char d[32][5]={"0","1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th",
"11th","12th","13rd","14th","15th","16th","17th","18th",
"19th",.....,"31st"}; // 請自己補全
char m[13][4]={" ","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
j = strlen(dmy);
printf("j=%d\n",j);
for (i=0;i<j ;i++) if ( dmy[i] =='/') dmy[i]=' ';
sscanf(dmy,"%d %d %d",&a,&b,&c);
printf("%s %s %d",d[a],m[b],c); // 列印出你要的 13rd Dec 2010

}

⑩ c語言怎麼將一個年月日轉換成秒數

用mktime()函數。

表頭文件:#include <time.h>

定義函數:time_tmktime(structtm*timeptr);

函數說明:mktime()用來將參數timeptr所指的tm結構數據轉換成從公元1970年1月1日0時0分0秒算起至今的UTC時間所經過的秒數。

返回值:返回經過的秒數。

(10)c語言日期轉換擴展閱讀:

C語言參考函數

C語言isgraph()函數:判斷一個字元是否是圖形字元

C語言isdigit()函數:判斷一個字元是否為數字

C語言iscntrl()函數:判斷一個字元是否為控制字元

C語言isalpha()函數:判斷一個字元是否是字母

C語言isalnum()函數:判斷一個字元是否是字母或者數字

C語言pow()函數:求x的y次方的值

C語言frexp()函數:提取浮點數的尾數和指數部分

熱點內容
centos使用python 發布:2024-05-18 23:39:48 瀏覽:866
幻影天龍腳本 發布:2024-05-18 23:38:17 瀏覽:711
編程的py 發布:2024-05-18 23:36:22 瀏覽:73
安卓系統怎麼改序列號 發布:2024-05-18 23:28:16 瀏覽:782
c語言中實數 發布:2024-05-18 23:21:03 瀏覽:894
伺服器搭建題目 發布:2024-05-18 23:01:29 瀏覽:27
下載武裝突襲後怎麼進伺服器 發布:2024-05-18 22:56:17 瀏覽:825
c語言字元串大寫變小寫 發布:2024-05-18 22:56:16 瀏覽:438
重啟刪除的文件夾 發布:2024-05-18 22:34:11 瀏覽:638
視頻軟體源碼 發布:2024-05-18 22:22:24 瀏覽:429