Determining Day of Week

Environment: Windows and Linux

Important Note

In the Gregorian calendar, which is widely used now, the year AD 1 is directly preceded by the year 1 BC; a year 0 does not exist in this system. In contrast, astronomical reckoning indeed uses a year 0. For the purpose of distinction, astronomical reckoning drops the symbols AD and BC and uses a plus or minus sign before the year instead. The astronomical year +1 therefore corresponds to the year AD 1, the year 0 corresponds to 1 BC, and the year -1 to 2 BC. For convenience, I'll use astronomical years both in this article and my program.

Overview

The program was based on the fact that the day of Jan 1, +1 is Monday, that of Jan 1, +2 is Tuesday, and so forth. That's because 365 % 7 = 1! With that knowledge, I get the program. I'll explain how I got that later, and hope it is useful to someone.

How and Why

Before I wrote the program, I noticed that there is no class or control to tell me what the day of Jan 1, +1 is, so I don't think it's easy to know what the day of a random date is in the traditional way. I must present a new approach or improve the old one, at least.

The traditional approach for calculating the day of a date is based on a date, the day of which was already known. Because the base day is randomly chosen, I can use a special date to make things easier. Jan 1 seems to be the best, thus I needn't care about how many days there are from the base date to the end of that year. Now which year shall I choose? Year 0 is the right one. Suppose we want to know the day of MM DD, YYYY. If we know that of Jan 1, YYYY, it is easy to get the answer. The expressions to calculate the day of Jan 1, YYYY will be:

theDayOfJan_1(YYYY) = ( theDayOfJan_1(0) + YYYY * 365 +
                        theCountOfFeb_29(YYYY) ) % 7

and theCountOfFeb_29(YYYY) is the number of days on February 29 that occur between year 0 and year YYYY.

Unfortunately, perhaps the result of "YYYY * 365" will overflow. But luckly, I just want the modulus. Since

( a + b + c ) % d = ( a %d + b % d + c % d ) % d,

and

( a * b ) % d = ( ( a % d ) * ( b % d ) ) %d,

so

( YYYY * 365 ) % 7 = ( ( YYYY % 7 ) * ( 365 % 7 ) ) % 7 = YYYY % 7,

theDayOfJan_1(YYYY) = ( theDayOfJan_1(0) % 7 + YYYY % 7 +
                        theCountOfFeb_29(YYYY) % 7 ) % 7,

But there is still a problem; theDayOfJan_1(0) remains unkown. Well, it will be the first date we calculated using the expression above, but not directly. The expression can be transformed into:

theDayOfJan_1(0) = ( theDayOfJan_1(YYYY) - YYYY % 7 -
                     theCountOfFeb_29(YYYY) % 7 + 14 ) % 7,

Assuming YYYY = +2000, and the day of Jan 1, +2000 is Saturday, then

theDayOfJan_1(2000) = 6,
YYYY % 7 = 2000 % 7 = 5,
theCountOfFeb_29(2000) = 484,

theDayOfJan_1(0) = ( 6 - 5 - 484 % 7 + 14 ) % 7 = 6,

That's to say the day of January 1, 0 is Saturday. Then that of January 1, +1 is Monday, and so on.

Downloads

Download source - 1.5 Kb



Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds