一个关于时间计算的问题
如何计算未来3天的日期?例如今天2011.06.28
输出:
2011.06.29
2011.06.30
2011.07.01
各位拜托啦 !小弟不才,跪求指导!
#include <stdio.h> #include <time.h> #define MAXSIZE 21 int main(void) { char str[MAXSIZE]; char * fmt = "%Y.%m.%d"; time_t t; time(&t); strftime(str, MAXSIZE, fmt, localtime(&t)); printf("%s\n", str); return 0; }
root@~ #cat lt94.c #include <stdio.h> #include <stdbool.h> struct date { int month; int day; int year; }; struct date dateUpdate (struct date today) { struct date tomorrow; int numberOfDays (struct date d); if(today.day != numberOfDays(today)) { tomorrow.day=today.day+1; tomorrow.month=today.month; tomorrow.year=today.year; } else if(today.month==12) { tomorrow.day=1; tomorrow.month=1; tomorrow.year=today.year+1; }else{ tomorrow.day=1; tomorrow.month=today.month+1; tomorrow.year=today.year; } return tomorrow; } int numberOfDays (struct date d) { int days; bool isLeapYear (struct date d); const int daysPerMonth[12]={ 31,28,31,30,31,30,31,31,30,31,30,31 }; if(isLeapYear(d)==true && d.month==2) { days=29; }else{ days=daysPerMonth[d.month-1]; } return days; } bool isLeapYear (struct date d) { bool leapYearFlag; if((d.year%4==0 && d.year%100!=0)||d.year%400==0) { leapYearFlag=true; }else{ leapYearFlag=false; } return leapYearFlag; } int main (void) { int i; struct date dateUpdate (struct date today); struct date today,nextday; printf ("Enter today's date (mm dd yyyy) :"); scanf ("%i%i%i",&today.month,&today.day,&today.year); for(i=0;i<3;i++) { nextday=dateUpdate(today); printf ("%.2i/%.2i/%i\n",nextday.month,nextday.day,nextday.year) ; today.day++; } return 0; }
root@~ #./lt94 Enter today's date (mm dd yyyy) :12 29 2011 12/30/2011 12/31/2011 01/01/2012 root@~ #