# 蔡勒公式

**蔡勒公式**（Zeller's congruence）是克里斯蒂安·蔡勒（Christian Zeller）设计的一种算法，它通过一个整数算术的单一闭式表达式，计算格里高利历或儒略历中任意日历日期是星期几，既不需要查表，也不需要循环。<sup>[1](https://xlinux.nist.gov/dads/HTML/zellersCongruence.html)</sup><sup> • </sup><sup>[2](https://github.com/Franceskynov/zeller)</sup> 蔡勒是德国数学家和新教牧师，于1882年至1886年间在一系列论文中发表了该公式，其中最著名的是1886年11月发表于《数学学报》（*Acta Mathematica*）的《日历公式》（"Kalender-Formeln"）。<sup>[3](https://de.wikisource.org/wiki/Seite:Acta_Mathematica_vol._009_(1886)_131.jpg)</sup>

| 关键事实 | 详情 |
|---|---|
| 用途 | 用一个整数算术表达式返回儒略历或格里高利历中任意日期是星期几<sup>[1](https://xlinux.nist.gov/dads/HTML/zellersCongruence.html)</sup><sup> • </sup><sup>[2](https://github.com/Franceskynov/zeller)</sup> |
| 发表 | 《日历公式》，《数学学报》第9卷，第131–136页，1886年11月；更早的版本见于1882年、1883年和1885年<sup>[3](https://de.wikisource.org/wiki/Seite:Acta_Mathematica_vol._009_(1886)_131.jpg)</sup><sup> • </sup><sup>[4](https://www.numdam.org/article/BSMF_1883__11__59_1.pdf)</sup> |
| 结果约定 | 在蔡勒最初的编号中，h = 0 为星期六，1 为星期日，6 为星期五<sup>[5](https://calendars.fandom.com/wiki/Zeller%27s_congruence)</sup> |
| 月份偏移 | 一月和二月被计为上一年中的第13个月和第14个月，从而将闰日折叠到一年的开头<sup>[3](https://de.wikisource.org/wiki/Seite:Acta_Mathematica_vol._009_(1886)_131.jpg)</sup> |
| 日历周期 | 格里高利历的星期模式每400年重复一次，因为400年包含146,097天，恰好是7的整数倍<sup>[6](https://cdn.azimpremjiuniversity.edu.in/media/publications/downloads/At-Right-Angles_Issue-17-Nov-2023_ONLINELow-Res.pdf)</sup> |
| 配套公式 | 同一篇1886年的论文还给出了复活节日期的同余式，蔡勒声称其到公元4200年都正确<sup>[3](https://de.wikisource.org/wiki/Seite:Acta_Mathematica_vol._009_(1886)_131.jpg)</sup> |

## 公式

蔡勒1886年的论文定义了五个量：J，世纪（去掉末两位数字的年份数）；K，世纪内的年份；m，月份；q，月内日期；h，星期编号。除法结果的小数部分被舍去，一月和二月被视为上一年的第13个月和第14个月。<sup>[3](https://de.wikisource.org/wiki/Seite:Acta_Mathematica_vol._009_(1886)_131.jpg)</sup> 星期h即为总和除以7后的余数：

- 儒略历：\[ h = \left( q + \frac{(m+1)\cdot 26}{10} + K + \frac{K}{4} + 5 - J \right) \bmod 7 \]
- 格里高利历：\[ h = \left( q + \frac{(m+1)\cdot 26}{10} + K + \frac{K}{4} + \frac{J}{4} - 2J \right) \bmod 7 \]

其中每个分数都表示整数（向下取整）除法。两个变体的区别仅在于世纪项，它们编码了两种日历不同的闰年规则：儒略历每四年增加一个闰日，而格里高利历则不将不能被400整除的世纪年算作闰年。<sup>[7](https://aa.usno.navy.mil/data/JulianDate)</sup>

**月份项为何有效。** 表达式 \( \frac{(m+1)\cdot 26}{10} \) 每月增长2.6，从而在累计总和中交替增加2或3，恰好与30天和31天月份的模式相匹配——一旦通过月份偏移将二月的不定长度移开。蔡勒指出，正是这一项使得将所有相关数字统一到一个公式中而无需辅助表格成为可能。<sup>[3](https://de.wikisource.org/wiki/Seite:Acta_Mathematica_vol._009_(1886)_131.jpg)</sup> J. R. 斯托克顿（J. R. Stockton）详细注释了蔡勒的历法论文，他观察到26/10对应每月30.6天，而另一种写法367/12（约每月30.583天）也同样可行。<sup>[8](https://people.cs.nycu.edu.tw/~tsaiwn/sisc/runtime_error_200_div_by_0/www.merlyn.demon.co.uk/zeller-c.htm)</sup>

**计算示例。** 对于1492年10月12日（儒略历），蔡勒自己的例子取 q = 12，m = 10，K = 92，J = 14，得到 \( 12 + 28 + 92 + 23 + 5 - 14 = 146 \)，其除以7的余数为6，因此该日期的星期编号为6，即星期五。<sup>[3](https://de.wikisource.org/wiki/Seite:Acta_Mathematica_vol._009_(1886)_131.jpg)</sup>

## 起源与发表

蔡勒于1882年开发了计算星期几和复活节日期的公式，并在四篇论文中发表：《用新的简化方法解决历法计算的基本问题》（Die Grundaufgaben der Kalenderrechnung auf neue und vereinfachte Weise gelöst）（1882年，载于《符腾堡州史季刊》*Württembergische Vierteljahrshefte für Landesgeschichte*）、《历法的两个基本问题》（Problema duplex Calendarii fundamentale）（1883年，载于《法国数学会通报》*Bulletin de la Société mathématique de France* 第11卷，第59–61页），以及《日历公式》的两个版本（1885年和1886年），后者发表于《数学学报》。<sup>[4](https://www.numdam.org/article/BSMF_1883__11__59_1.pdf)</sup><sup> • </sup><sup>[8](https://people.cs.nycu.edu.tw/~tsaiwn/sisc/runtime_error_200_div_by_0/www.merlyn.demon.co.uk/zeller-c.htm)</sup> 每篇论文都有两个部分，一部分讨论星期几，一部分讨论复活节星期日，涵盖两种日历和任意公元年份。<sup>[8](https://people.cs.nycu.edu.tw/~tsaiwn/sisc/runtime_error_200_div_by_0/www.merlyn.demon.co.uk/zeller-c.htm)</sup> 蔡勒还将他的规则印成明信片送给家人，题为《日历计算大全》（"Das Ganze der Kalender-Rechnung"）。<sup>[9](https://merkel-zeller.de/getperson.php?personID=I3332&tree=Merkel-Zeller)</sup>

爱德华·M. 赖因戈尔德（Edward M. Reingold）和内胡姆·德肖维茨（Nachum Dershowitz）的参考著作《历法计算》（*Calendrical Calculations*）将蔡勒描述为19世纪下半叶德国的一位新教牧师和神学院院长，并指出他的方法常用于“心算敏捷”的表演中。<sup>[10](https://rexresearch1.com/CalendarsLibrary/Calendrical%20CalculationsDershowitz.pdf)</sup>

## 实现注意事项

**负数取模陷阱。** 蔡勒期望表达式由手工计算，其中负的中间值通过加上7的倍数来处理。计算机实现的取模运算符通常不是这样运作的；许多语言将 \( (-A) \bmod B \) 计算为 \( -(A \bmod B) \)，产生负数结果。已知有不够谨慎的实现出现错误，该错误可能影响每个世纪初期三月以后的一部分日期。标准的修正方法是将负项替换为等价的正项（蔡勒本人在1882年就这样做了，例如在儒略历形式中写 \( +5 - J \) 而非 \( -2J \)），或者对负结果加7。<sup>[8](https://people.cs.nycu.edu.tw/~tsaiwn/sisc/runtime_error_200_div_by_0/www.merlyn.demon.co.uk/zeller-c.htm)</sup><sup> • </sup><sup>[11](https://web.cs.dal.ca/~jamie/CS3172/Course/assig/zeller.html)</sup>

**星期编号。** 蔡勒将日子从星期日 = 1 编号到星期六 = 7，星期六也可表示为0，因此 h = 0 意味着星期六。这与ISO 8601不同，后者将星期一编号为1到星期日为7；转换为 \( ((h+5) \bmod 7) + 1 \)。<sup>[8](https://people.cs.nycu.edu.tw/~tsaiwn/sisc/runtime_error_200_div_by_0/www.merlyn.demon.co.uk/zeller-c.htm)</sup> 在将数值结果读作星期名称之前，通过正确的表格进行映射是常见的错误来源，因为许多编程日期库使用的是0 = 星期日或1 = 星期一。<sup>[12](https://freeonlinecalc.net/guides/day-of-week-from-date-algorithm/)</sup>

**范围与有效性。** 该公式隐含地编码了每种日历的闰年规则，因此格里高利历版本不得未经调整就应用于儒略历日期，反之亦然。<sup>[13](https://every-algorithm.github.io/2024/04/16/zellers_congruence.html)</sup> 斯托克顿报告称，在遵守上述取模注意事项的前提下，蔡勒的星期几算法对公元1年以来的所有年份都准确，而更早的年份可以通过加上日历重复间隔的倍数来处理——儒略历星期周期为28年，格里高利历为400年。<sup>[8](https://people.cs.nycu.edu.tw/~tsaiwn/sisc/runtime_error_200_div_by_0/www.merlyn.demon.co.uk/zeller-c.htm)</sup>

该公式已在许多语言中实现，包括C、Pascal、JavaScript、Python、Java和Haskell，并作为从日期查找星期几的标准答案之一出现在comp.lang.c常见问题中。<sup>[1](https://xlinux.nist.gov/dads/HTML/zellersCongruence.html)</sup><sup> • </sup><sup>[14](https://www.lysator.liu.se/faq/c-faq/c-17.html)</sup> 在生产环境的C程序中，标准库（例如 `mktime`）通常起到相同的作用，因此蔡勒公式如今主要作为教学示例和心算工具发挥作用。<sup>[15](https://www.codedrome.com/calculating-the-day-of-the-week-with-zellers-congruence-in-c/)</sup>

## 相关方法

任何星期几算法都是以闭式表达式对天数进行模7计数，已知公式的区别在于如何将不规则的月份长度和闰年规则折叠为几个整数项。<sup>[16](https://djangoproject.in/blog/zellers-congruence/)</sup> 同样的原理也是天文学中使用的儒略日的基础，其中星期几是连续天数除以7的余数。<sup>[7](https://aa.usno.navy.mil/data/JulianDate)</sup> 替代方法包括坂本知彦（Tomohiko Sakamoto）基于查表的简短C例程（发表于comp.lang.c常见问题），以及约翰·康威（John Conway）的末日算法（Doomsday algorithm，1973年），后者计算4/4、6/6、8/8、10/10和12/12等易记锚定日期的星期几，专为心算设计。<sup>[14](https://www.lysator.liu.se/faq/c-faq/c-17.html)</sup><sup> • </sup><sup>[17](https://people.csail.mit.edu/ddeford/Crossroads_pdfs/Date_Methods.pdf)</sup> 蔡勒公式直接从日期的数字输出星期几，而末日算法依赖记忆的锚点；两者都利用了格里高利历的周期性。<sup>[17](https://people.csail.mit.edu/ddeford/Crossroads_pdfs/Date_Methods.pdf)</sup>

## References

1. [Paul E. Black, "Zeller's congruence", Dictionary of Algorithms and Data Structures, NIST](https://xlinux.nist.gov/dads/HTML/zellersCongruence.html)
2. [Franceskynov/zeller, GitHub repository description](https://github.com/Franceskynov/zeller)
3. [Christian Zeller, "Kalender-Formeln", Acta Mathematica 9 (1886), 131–136, Wikisource full text](https://de.wikisource.org/wiki/Seite:Acta_Mathematica_vol._009_(1886)_131.jpg)
4. [Ch. Zeller, "Problema duplex Calendarii fundamentale", Bulletin de la S. M. F. 11 (1883), 59–61](https://www.numdam.org/article/BSMF_1883__11__59_1.pdf)
5. [Zeller's congruence, Calendar Wiki](https://calendars.fandom.com/wiki/Zeller%27s_congruence)
6. [Anushka Tonapi, "Zeller's Congruence", At Right Angles, Azim Premji University, November 2023](https://cdn.azimpremjiuniversity.edu.in/media/publications/downloads/At-Right-Angles_Issue-17-Nov-2023_ONLINELow-Res.pdf)
7. [Julian Date Converter, US Naval Observatory](https://aa.usno.navy.mil/data/JulianDate)
8. [J R Stockton, "Zeller's Calendrical Works" and "On Zeller's 1886 Paper"](https://people.cs.nycu.edu.tw/~tsaiwn/sisc/runtime_error_200_div_by_0/www.merlyn.demon.co.uk/zeller-c.htm)
9. [Merkel-Zeller genealogy, entry for Julius Christian Johannes Zeller](https://merkel-zeller.de/getperson.php?personID=I3332&tree=Merkel-Zeller)
10. [Reingold & Dershowitz, Calendrical Calculations (PDF excerpt)](https://rexresearch1.com/CalendarsLibrary/Calendrical%20CalculationsDershowitz.pdf)
11. [J. Blustein, Calendar Algorithms assignment, Dalhousie University](https://web.cs.dal.ca/~jamie/CS3172/Course/assig/zeller.html)
12. [How to work out the day of the week for any calendar date, Free Online Calc](https://freeonlinecalc.net/guides/day-of-week-from-date-algorithm/)
13. [Zeller's Congruence: Calculating Days of the Week, Every Algorithm](https://every-algorithm.github.io/2024/04/16/zellers_congruence.html)
14. [comp.lang.c FAQ, question 17.28](https://www.lysator.liu.se/faq/c-faq/c-17.html)
15. [Calculating the Day of the Week with Zeller's Congruence in C, CodeDrome](https://www.codedrome.com/calculating-the-day-of-the-week-with-zellers-congruence-in-c/)
16. [Zeller's Congruence: Day of the Week Formula in Python](https://djangoproject.in/blog/zellers-congruence/)
17. [Doomsday Method and Zeller's Congruence, Crossroads (MIT CSAIL PDF)](https://people.csail.mit.edu/ddeford/Crossroads_pdfs/Date_Methods.pdf)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026; Oct 25, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 测量与时间 › 历法 › Calendar mechanics and reform › Julian–Gregorian conversion and calendar correspondence

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/zellers-congruence

English original: https://www.edgepedia.org/zellers-congruence
