基姆拉尔森计算公式
W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7
在公式中d表示日期中的日数,m表示月份数,y表示年数。
注意:在公式中有个与其他公式不同的地方:
把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。
/// 计算具体某个日期是星期几 经实践验证,这个方法数据有误
public string CaculateWeekDay(int y, int m, int d)
{
if (m == 1 || m == 2)
{
m += 12;
y--;
}
int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
string weekstr = "";
switch (week)
{
case 0: weekstr = "星期一"; break;
case 1: weekstr = "星期二"; break;
case 2: weekstr = "星期三"; break;
case 3: weekstr = "星期四"; break;
case 4: weekstr = "星期五"; break;
case 5: weekstr = "星期六"; break;
case 6: weekstr = "星期七"; break;
}
return weekstr;
}
c++ 由上面代码修改
void CaculateWeekDay(int y, int m, int d)
{
static int num = 0;
if (m == 1) m = 13;
if (m == 2) m = 14;
int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7 + 1;
int value_day = 0;
switch (week)
{
case 1: value_day = 0; break;
case 2: value_day = 1; break;
case 3: value_day = 2; break;
case 4: value_day = 3; break;
case 5: value_day = 4; break;
case 6: value_day = 5; break;
case 7: value_day = 6; break;
}
cout << "CaculateWeekDay num+++++++++++++++++++++++++++" << num << endl;
//week_days.push_back(value_day);
//date_map.insert(pair<int, int>(num, value_day));
//num++;
}