分类:
2011-12-15 11:04:50
原文地址:计算某天是星期几【C代码】 作者:ammana_babi
【本程序在DEV C++ 4.9.9.2 下编译通过】
有关农历的东西有以下几篇文章:
#include
/*
函数名称:int GetWeekDay(int year,int month,int day)
函数功能: 返回输入日期对应的一周中的第几天。
函数参数: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
//下面仅仅是拿当前的日期作为计算,当然你也可以用来计算其他的日期。
//如: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日\t",year,month,day);
puts(Week[GetWeekDay(year,month,day)]);
//下面是访问tm结构体来得到当前是星期几
printf("%d年%d月%d日\t",year,month,day);
puts(Week[Local->tm_wday]);
#else
year = 2006;
month = 10;
day = 1;
printf("%d年%d月%d日\t",year,month,day);
puts(Week[GetWeekDay(year,month,day)]);
#endif
system("pause");
}