#include <stdio.h>
static char daytab[2][13]= {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
/************************************************************************/
/* day_of_year : set day of year from month & day
/************************************************************************/
int day_of_year(int year, int month, int day){
int i, leap;
leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
for (i = 1; i < month ; i++) {
day += daytab[leap][i];
}
return day;
}
/************************************************************************/
/* set month, day from day of year
/************************************************************************/
void month_day(int year, int yearday, int *pmonth, int *pday){
int i, leap;
leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
for (i = 1; yearday > daytab[leap][i]; i ++) {
yearday -= daytab[leap][i];
}
*pmonth = i;
*pday = yearday;
}
int main(){
int month, day;
printf("%d\n", day_of_year(2011, 9, 29));
month_day(2011, 272, &month, &day);
printf("%d\t%d\n", month, day);
return 0;
}
C语言实现日期和一年中的天数的相互转化
最新推荐文章于 2022-07-27 10:34:09 发布