0
$\begingroup$

I have a confusion about the way of solving the following mathematical problem:

If Mr. X was born on April 16, 1987 what day is 2016 days after he was born?

How will I solve these kind of problem? I mean what is the general rule?

$\endgroup$
  • $\begingroup$ Duplicate. Eg math.stackexchange.com/questions/16945/… $\endgroup$ – almagest Apr 16 '16 at 17:08
  • $\begingroup$ @almagest That question asks for the day of a week, given a date. This question asks for essentially the resulting date, given a delta of days and an initial date. Do you think those questions have the same answer/algorithm? $\endgroup$ – Words Like Jared Apr 16 '16 at 22:22
2
$\begingroup$

$2016 = 365 \times 5+191$

So 5 years and 189 days have elapsed (191-2 days because there are two leap year, one every 4 years, except when the year number is divisible by 4, and 100 but not by 400).

$189=6 \times 30 +9$

So an approximate date is October the 25th. However, months aren't all 30 days, so we have to take that into account.

April has 30 days May has 31 June has 30 July has 31 August has 30 September has 31

So we overestimated the date by 3 days.

Thus, the real date is October the 22th, 1992.

Also, $2016 \equiv 0$ mod 7 so it is the same week day (Thursday)

So as a summary, I would say the rules are

-Count the number of years elapsed

-Count the number of days remaining taking care of leap years which happen every four years

-Count the number of months elapsed in the remaining number of days, and take into account the real month duration (if February is within the interval and the current year is a leap one, make sure you count it as 29 days instead of 28)

-To know the day of the week assuming you know one of them (either the one at the start or the one and the end), take the number of total days modulo 7.

EDIT : there are actually 2 leap years because 1987 is 3 mod 4 (and 1988 and 1992 are non-zero mod 100), which means 1988 is a leap year and so is 1992 !

$\endgroup$
  • 1
    $\begingroup$ While it does not affect your answer, August has $31$ days and September has $30$ days. $\endgroup$ – N. F. Taussig Apr 16 '16 at 21:14
0
$\begingroup$

You asked for the general rule: Suppose you have some valid date (consisting of a year, month and day) and you want to know how to add a positive number of days to it and compute the resulting date.

Assuming that the Gregorian calendar[*] is the model you're using, you can do this like this. First define a few helper functions:

IsLeapYear(year) is a function returns whether or not a year is a leap year that adheres to the following:

Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400.

ConvertYearToDays(year) is a function that returns how many days are in a year that adheres to the following:

If IsLeapYear(year) return 366. Otherwise return 365.

ConvertMonthToDays(year, month) is a function that returns how many days are in a month for a particular that adheres to the following:

If month is January, March, May, July, August, October, December return 31. If month is April, June, September, October, November return 30. Otherwise the month must be February, in which case you return 29 if IsLeapYear(year), otherwise 28.

Step 1: Figure out how many days, elapsed your date into the year beyond January 1. Call ConvertMonthToDays on each of the months that have elapsed, and call that elapsed. Now add elapsed to days and call that remaining. Keep a running year and month (initialized to January and the year of the supplied date, respectively).

Step 2: Determine the year's number of days, ConvertYearToDays(year). If you have less than that in remaining, go to the next step. Otherwise increment the year by 1 and remove that many days from your remaining. Go back to the beginning of this step.

Step 3: Determine the month's number of days, ConvertMonthToDays(year, month). If you have less than that in remaining, go to the next step. Otherwise increment the month by 1 and remove that many days from your remaining. Go back to the beginning of this step.

Step 4: Your final date should be the values you have in year, month and remaining for the days.

Basically we came up with how many days to distribute (remaining) and then removed larger chunks from it, first, starting with a year, then a month, and then what was left was the days (if you needed to compute larger time spans, such as 400+ years' worth of days, it'd be good to have a step which can take care of maybe 400 years at once, since that is the full cycle of the Gregorian Calendar).

[*] If you wanted to compute dates used by past humans, note the Gregorian calendar may not have been used/"official" calendar dates you'd see on their documents would end up being different. Also note that the length of a "day" and "year" in seconds probably changes throughout the history of our solar system, so for sufficiently far enough to the past or future you'd have to take that into account and our notion of how we measure time will break down.

If I needed to do this a lot, I'd probably just write a program, because hopefully Microsoft will update their framework to take care of the nuances of Gregorian calendar when it breaks down in the future (this is C# which you could run on https://dotnetfiddle.net/):

// April 16, 1987
var birthday = new DateTime(1987, 4, 16);

// 2016 days later
var later = birthday.AddDays(2016);

// 10/22/1992 12:00:00 AM
Console.WriteLine(later);
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.