# A revised algorithm for converting Gregorian dates to day counts

DevFeed: [A revised algorithm for converting Gregorian dates to day counts](<https://devfeed.tech/articles/counting-the-days-revisited-36232.md>)

Original publisher: [Read original article](<https://dotat.at/@/2026-08-09-rata-die.html>)

Published: 2026-08-09T02:29:48Z

Content type: article

Language: en

Sources: [Tony Finch's blog](<https://devfeed.tech/sources/tony-finch-s-blog.md>)

Topics: [Algorithm](<https://devfeed.tech/topics/algorithm.md>), [DateTime](<https://devfeed.tech/topics/datetime.md>), [C](<https://devfeed.tech/topics/c.md>), [Code](<https://devfeed.tech/topics/code.md>), [function](<https://devfeed.tech/topics/function.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>)

Tags: [algorithm](<https://devfeed.tech/tags/algorithm.md>), [c](<https://devfeed.tech/tags/c.md>), [code](<https://devfeed.tech/tags/code.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [data-type](<https://devfeed.tech/tags/data-type.md>), [function](<https://devfeed.tech/tags/function.md>), [range](<https://devfeed.tech/tags/range.md>)

## AI overview

The article revisits an algorithm for converting Gregorian dates into Julian Day numbers or related day counts such as rata die. It explains the March-based month pattern, leap-year corrections, integer arithmetic, and limitations caused by overflow in the output data type.

## Source excerpt

Many years ago I wrote about how to convert Gregorian dates to Julian Day numbers or similar counts such as rata die as used in Calendrical Calculations. This algorithm is the core of C's mktime() function that converts a broken-down date-time into linear time_t. I recently learned from Ben Joffe that I was missing a few tricks, and my old code wasn't as good as it could have been. Here's a better version (using conventional not C numbering): if m > 2 { m -= 2; } else { m += 10; y -= 1; } y*365 + y/4 - y/100 + y/400 + m*979/32 + d - 336 the main idea Julian years Gregorian correction the month pattern the epoch domains and ranges leap year test length of month the main idea There's a helpful coincidence in the Gregorian calendar. Although the month lengths aren't obviously regular, there's a repeating 5 month pattern that becomes easier to see when you start from March, as illustrated by the table below. This pattern resets at the end of February, midway through its third repeat, coincidentally at the same point that leap days occur. Thus the first line of the code above adjusts the month and year numbers so that January and February are counted at the end of the previous year, and the coincidental alignment occurs at the boundary between the adjusted year numbers. I'll explain the details of the adjustment as I discuss the relevant parts of the second line March 31 days April 30 days May 31 days June 30 days July 31 days August 31 days September 30 days October 31 days November 30 days December 31 days January 31 days February 28 or 29 Julian years The first part of the main formula counts the number of days before the start of year y, in terms of normal years and leap days. y * 365 + y / 4 The adjustment subtracts one from the year in January and February. The effect is that the leap day in year 4 is counted as a day before the start of the adjusted beginning of year 4, i.e. before March, i.e. exactly the right place. I previously combined this part of the express