1、一周中的第几天
char * dayOfTheWeek(int day, int month, int year){
char *week[]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int monthday[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int cnt=4;
int i;
for(i=1971;i<year;++i){
if(i%4==0&&i%100||i%400==0){
cnt+=366;
}else{
cnt+=365;
}
}
for(i=1;i<month;++i){
if(i==2){
if(year%4==0&&year%100||year%400==0){
cnt+=29;
}else{
cnt+=monthday[i];
}
}else{
cnt+=monthday[i];
}
}
cnt+=day;
cnt%=7;
return week[cnt];
}
2、一年中的第几天
bool isLeapYear(int y){
return (y%4==0&&y%100||y%400==0);
}
int strTotal(char *str,int len){
int sum=0;
for(int i=0;i<len;++i){
sum = sum*10+(str[i]-'0');
}
return sum;
}
int dayOfYear(char * date){
int monthday[12]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int sumday[13];
int year,month,day;
year=strTotal(date+0,4);
month=strTotal(date+5,2);
day=strTotal(date+8,2);
monthday[2]=isLeapYear(year)?29:28;
sumday[0]=0;
for(int i=1;i<month;++i){
sumday[i]=sumday[i-1]+monthday[i];
}
return sumday[month-1]+day;
}
3、日期之间隔几天
int strTotal(char *date){
int y,m,d;
int monthday[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int cnt=0;
sscanf(date,"%d-%d-%d",&y,&m,&d);
int i;
for(i=1970;i<y;++i){
if(i%4==0&&i%100||i%400==0){
cnt+=366;
}else{
cnt+=365;
}
}
for(i=1;i<m;++i){
if(i==2){
if(y%4==0&&y%100||y%400==0){
cnt+=29;
}else{
cnt+=28;
}
}else{
cnt+=monthday[i];
}
}
cnt+=d;
return cnt;
}
int daysBetweenDates(char * date1, char * date2){
return abs(strTotal(date1)-strTotal(date2));
}