Skip
# How can I calculate a month/date delta?

(adsbygoogle = window.adsbygoogle || []).push({});

posted by zengargoyle at 9:10 AM on March 25, 2011 [5 favorites]

(adsbygoogle = window.adsbygoogle || []).push({});

Post

# How can I calculate a month/date delta?

March 25, 2011 8:36 AM Subscribe

Calendar math question: today is March 25, 2011. I found my old watch in a drawer and the time is still correct, but the watch date window says 13. Assuming the date window repeats 1 to 31 and that I always 'set' my watch correctly, what was the most recent month and year that I could have worn it?

(This isn't a homework question or quiz answer or anything - this is a legit curiosity.)

And the (B) question would be: Is there an easy way of calculating this?

(This isn't a homework question or quiz answer or anything - this is a legit curiosity.)

And the (B) question would be: Is there an easy way of calculating this?

This is just a day-of-month number, right, and not both month and day?

Anyway, a 31-day month would translate to 372 days per year, so it would gain 7 days in non-leap years and 6 in leap years. This means your hypothetical date is

I don't think there's an easy way of calculating it, because of the variable length of individual months. Easiest way to solve would be a spreadsheet using a date function and then a column search.

posted by dhartung at 8:44 AM on March 25, 2011

Anyway, a 31-day month would translate to 372 days per year, so it would gain 7 days in non-leap years and 6 in leap years. This means your hypothetical date is

*about*three years ago.I don't think there's an easy way of calculating it, because of the variable length of individual months. Easiest way to solve would be a spreadsheet using a date function and then a column search.

posted by dhartung at 8:44 AM on March 25, 2011

I don't know if there's an

posted by adamrice at 8:50 AM on March 25, 2011

*elegant*way, but the tedious way I'd calculate it is this: The number of days in each month Janâ€“Dec is 31 28 31 30 31 30 31 31 30 31 30 31 (I'm ignoring leap years). Find the current month and take the difference between 31 and the actual number of days in the month. Add that number to a tally. Then go back a month and repeat. Keep going until your tally equals 14 (25 (actual date) - 13 (apparent date)).posted by adamrice at 8:50 AM on March 25, 2011

So if you'd last worn it in February 2011, today it would say 22 -- that's 25 minus 3, since there are three "missing" days (29th, 30th, 31st) at the end of February.

Now in an entire year there are

Keep going backward from there -- you lose another day at the end of November 2009, bringng it to 14, and another at the end of September 2009, brining it to 13. The answer is September 2009, although it's possible you last wore it in July or August of last year.

posted by madcaptenor at 9:06 AM on March 25, 2011 [1 favorite]

Now in an entire year there are

*seven*"missing" days -- (12 * 31) - 365 -- and so if you'd last worn it in February 2010, it would say 22-7, or 15.Keep going backward from there -- you lose another day at the end of November 2009, bringng it to 14, and another at the end of September 2009, brining it to 13. The answer is September 2009, although it's possible you last wore it in July or August of last year.

posted by madcaptenor at 9:06 AM on March 25, 2011 [1 favorite]

I did a spreadsheet as dhartung recommended, and I'm getting the date in September 2009 (and therefore also August and July). Take with a grain of salt, because I'm no excel expert, but if I just write out the dates that are 31 days previous from today, I get the same answer.

posted by specialagentwebb at 9:09 AM on March 25, 2011

posted by specialagentwebb at 9:09 AM on March 25, 2011

`days=31; while [ $(date -d "$days days ago" +%d) != 13 ]; do days=$(($days + 31)); done; date -d "$days days ago"; echo $days; echo $(($days / 31 ))`

Sun Sep 13 09:08:24 PDT 2009

posted by zengargoyle at 9:10 AM on March 25, 2011 [5 favorites]

Using something similar to Adam Rice's method, I also got September 2009.

We start with a 12-day differential. February subtracts 3 days unless it's a leapyear. Novembers, Septembers, Junes, and Aprils subtract one day. Count backwards until you get to 0.

Feb 2011: 9

Nov,Sept,Jue, Apr 2010: 5

Feb 2010: 2

Nov,

posted by muddgirl at 9:32 AM on March 25, 2011

We start with a 12-day differential. February subtracts 3 days unless it's a leapyear. Novembers, Septembers, Junes, and Aprils subtract one day. Count backwards until you get to 0.

Feb 2011: 9

Nov,Sept,Jue, Apr 2010: 5

Feb 2010: 2

Nov,

**Sept 2009**: 0posted by muddgirl at 9:32 AM on March 25, 2011

I just thought of an edge case that makes this harder to solve as a general problem.

What if the put-away date is Feb 28, and the actual date is Mar 1? The watch will show 29. For one thing, you get a negative number as the difference between the actual date and apparent date (1 - 29 = -28), although you can take the absolute value, which I think solves that part of the problem. But the bigger problem is that (at least with the method I proposed), there's no algorithmic way to account for the 'overhang' if you put the watch away at the end of a short month. Am I missing something?

posted by adamrice at 9:35 AM on March 25, 2011

What if the put-away date is Feb 28, and the actual date is Mar 1? The watch will show 29. For one thing, you get a negative number as the difference between the actual date and apparent date (1 - 29 = -28), although you can take the absolute value, which I think solves that part of the problem. But the bigger problem is that (at least with the method I proposed), there's no algorithmic way to account for the 'overhang' if you put the watch away at the end of a short month. Am I missing something?

posted by adamrice at 9:35 AM on March 25, 2011

adamrice: not the absolute value! In the general case, for 1-29 you want to use 1-29+31 = 3. Work mod 31.

(For example, this watch would have shown 25 nineteen days ago, on March 6; the difference is then 6-25 = -19, which we should treat as 31-19 = 12.)

posted by madcaptenor at 10:34 AM on March 25, 2011

(For example, this watch would have shown 25 nineteen days ago, on March 6; the difference is then 6-25 = -19, which we should treat as 31-19 = 12.)

posted by madcaptenor at 10:34 AM on March 25, 2011

madcaptenorâ€”right, I knew I was missing something. Although working mod 31, the math still don't come out right. If you work mod [number of days in the previous month] I think it does.

posted by adamrice at 11:06 AM on March 25, 2011

posted by adamrice at 11:06 AM on March 25, 2011

This thread is closed to new comments.

posted by Heart_on_Sleeve at 8:42 AM on March 25, 2011