Data
Data

Reputation: 769

Calculating which day of the week a date falls on using Gauss's algorithm, ordinal date and modulo arithmetic

After calculating which day of the week the 1st of January falls on using Gauss's algorithm, as well as calculating the ordinal date for a given calendar date, how can the day of the week of the latter date be calculated?

For example, Gauss's algorithm can tell us that, this year, the 1st of January fell on a Sunday, the 7th day of the week. Today is the 22nd of October, with an ordinal day of 295. How can this information be used to calculate that today is a Sunday?

Upvotes: 0

Views: 637

Answers (2)

Data
Data

Reputation: 769

An approach I've found, which I haven't tested extensively, but seems to work with the dates I've thrown at it, is...

(ordinal day + day of 1st of January - 1) % 7

Where Mon = 1, Tue = 2,..., Sat = 6, Sun = 0.

In the example mentioned in the question:

(295 + 0 - 1) % 7 = 0 (Sunday)

Upvotes: 0

Axel Kemper
Axel Kemper

Reputation: 11322

For common years (= non-leap years), 1st of January and 1st of October are on the same day of the week:

Jan  31
Feb  28
Mar  31
Apr  30
May  31
Jun  30
Jul  31
Aug  30
Sep  31
Sum 273 = 39 x 7

See Wikipedia

22nd October is exactly three weeks later than 1st of October.

Upvotes: 0

Related Questions