# 日历查询的算法 如何计算某一天是星期几

—— 蔡勒（Zeller）公式

=49+[49/4]+[20/4]-2×20+[26× (10+1)/10]+1-1
=49+[12.25]+5-40+[28.6]
=49+12+5-40+28
=54 (除以7余5)

[(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400]，

[...]表示只取整数部分。第一项表示需要加上被4整除的年份数，第二项表示需要去掉被100整除的年份数，第三项表示需要再加上被400整除的年份数。之所以Y要减一，这

W = (Y-1)*365 + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + D． (1)

W = (2004-1)*365 + [(2004-1)/4] - [(2004-1)/100] + [(2004-1)/400] +(31+29+31+30+1)= 731702，
731702 / 7 = 104528……6，余数为六，说明这一天是星期六。这和事实是符合的。

(Y-1)*365 = (Y-1) * (364+1)
= (Y-1) * (7*52+1)
= 52 * (Y-1) * 7 + (Y-1)，

(Y-1)*365 ≡ Y-1 (mod 7)．

W = (Y-1) + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + D． (2)

--------------------------------------------------------------------------

------------------------------------------------------------------------

╭ d； （当M＝1）
D = { 31 + d； （当M＝2） (3)
╰ [ 13 * (M+1) / 5 ] - 7 + (M-1) * 28 + d + i． （当M≥3）

D = [ 13 * (5+1) / 5 ] - 7 + (5-1) * 28 + 1 + 1
= 122，

D = [ 13 * (M+1) / 5 ] - 7 + (M-1) * 28 + d． （3≤M≤14） (4)

W = (Y-1) + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + [ 13 * (M+1) / 5 ] - 7 + (M-1) * 28 + d．

W = (Y-1) + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + [ 13 * (M+1) / 5 ] + d．(5)

W = (2003-1) + [(2003-1)/4] - [(2003-1)/100] + [(2003-1)/400] + [13*(13+1)/5]
+ 1
= 2002 + 500 - 20 + 5 + 36 + 1
= 2524；
2524 / 7 = 360……4．这和实际是一致的。

--------------------------------------------------------------------

==============================================

--------------------------------------------------------------------

W = (4 - C mod 4) * 2 - 4． (6)

W = (4 - 20 mod 4) * 2 - 4
= 8 - 4
= 4．

(Y-1) + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] ≡ (4 - C mod 4) * 2 - 1
(mod 7)． (7)

W = (4 - C mod 4) * 2 - 1 + [13 * (M+1) / 5] + d． (8)

W = (4 - C mod 4) * 2 - 1 + (y-1) + [y/4] + [13 * (M+1) / 5] + d． (9)

4q + r = C，

r = C - 4q
= C - 4 * [C/4]． (10)

(4 - C mod 4) * 2 ＝ (4 - C + 4 * [C/4]) * 2
＝ 8 - 2C + 8 * [C/4]
≡ [C/4] - 2C + 1 (mod 7)． (11)

W = [C/4] - 2C + y + [y/4] + [13 * (M+1) / 5] + d - 1． (12)

W = [20/4] - 40 + 4 + 1 + [13 * (5+1) / 5] + 1 - 1
= -15．

W = 5 - C + y + [y/4] + [13 * (M+1) / 5] + d - 1． (13)

posted on 2015-05-26 09:28 carekee 阅读(...) 评论(...)  编辑 收藏