計(jì)算某天是星期幾【C代碼】
有關(guān)農(nóng)歷的東西有以下幾篇文章:
農(nóng)歷一百年算法(1921~2021)【C語(yǔ)言代碼】
農(nóng)歷兩百年算法(1901~2100)【C語(yǔ)言代碼】
#include <time.h>
/*
函數(shù)名稱(chēng):int GetWeekDay(int year,int month,int day)
函數(shù)功能: 返回輸入日期對(duì)應(yīng)的一周中的第幾天。
函數(shù)參數(shù):year 輸入日期的年;month 輸入日期的月;day輸入日期的日
如:2000年1月1日則是GetWeekDay(2000,1,1)
*/
int GetWeekDay(int year,int month,int day)
{
int i,j,count=0;
int Day_Index;
int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int MonthAdd[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
#if 0
for(i = 1;i < month;i++)
count = count + days[i];
#else
count = MonthAdd[month-1];
#endif
count = count + day;
if((year%4 == 0&&year %100 != 0||year % 400 == 0)&& month >= 3)
count += 1;
count = count + (year - 1901) * 365;
for(j = 1901;j < year;j++)
{
if(j % 4 == 0 && j % 100 != 0 || j % 400 == 0)
count++;
}
return ((count+1) % 7);
}
main()
{
const char *Week[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
struct tm * Local;
long t;
int year,month,day;
#if 0
//下面僅僅是拿當(dāng)前的日期作為計(jì)算,當(dāng)然你也可以用來(lái)計(jì)算其他的日期。
//如:2000年1月1日則是GetWeekDay(2000,1,1)
t = time(NULL);
Local = localtime(&t);
year = Local->tm_year + 1900;
month = Local->tm_mon + 1;
day = Local-> tm_mday;
printf("%d年%d月%d日\(chéng)t",year,month,day);
puts(Week[GetWeekDay(year,month,day)]);
//下面是訪問(wèn)tm結(jié)構(gòu)體來(lái)得到當(dāng)前是星期幾
printf("%d年%d月%d日\(chéng)t",year,month,day);
puts(Week[Local->tm_wday]);
#else
year = 2006;
month = 10;
day = 1;
printf("%d年%d月%d日\(chéng)t",year,month,day);
puts(Week[GetWeekDay(year,month,day)]);
#endif
system("pause");
}