Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Date arithmetic seem to be a complicated matter.

Around 2.5 years ago I sent the following feedback to the Wolfram|Alpha Feedback Team, never heard back from them.

    Message: When I compute
    "2019-01-31 to 2016-04-04" I get "2 years 9 months 26 days"
    and when I compute the reversed input
    "2016-04-04 to 2019-01-31" I get "2 years 9 months 27 days"
    
    But when I compute
    "2019-01-31 to 2015-10-21" I get "3 years 3 months 10 days"
    and when I compute the reversed input
    "2015-10-21 to 2019-01-31" I get "3 years 3 months 10 days"
    
    Shouldn't the very first one ( "2019-01-31 to 2016-04-04" ) also return "2 years 9 months 27 days"?


Inverting the date range probably confused the logic involved in accounting for leap dates. Possibly it fails to account for swapping the order of the dates to achieve an always positive result?


It's not (only?) related to leap years. It appears to be related to the month of April and some other weird stuff.

    -- leap year 2012:
    2019-01-31 to 2012-01-30 --> 7 years 1 day
    2012-01-30 to 2019-01-31 --> 7 years 1 day

    2019-01-31 to 2012-02-29 --> 6 years 11 months         <---- Weird stuff
    2012-02-29 to 2019-01-31 --> 6 years 11 months 3 days  <---- Weird stuff

    2019-01-31 to 2012-02-30 --> 6 years 10 months 30 days (2012-02-30 does not exist)
    2012-02-30 to 2019-01-31 --> 6 years 10 months 30 days (2012-02-30 does not exist)

    2019-01-31 to 2012-03-30 --> 6 years 10 months 1 day
    2012-03-30 to 2019-01-31 --> 6 years 10 months 1 day

    2019-01-31 to 2012-04-30 --> 6 years 9 months          <---- April (any day in April)
    2012-04-30 to 2019-01-31 --> 6 years 9 months 1 day

    2019-01-31 to 2012-05-01 --> 6 years 8 months 30 days
    2012-05-01 to 2019-01-31 --> 6 years 8 months 30 days

    -- non-leap year 2013:
    2019-01-31 to 2013-04-30 --> 5 years 9 months          <---- April (any day in April)
    2013-04-30 to 2019-01-31 --> 5 years 9 months 1 day

I stumbled upon it while testing some JavaScript time and date frameworks and wanted to use Wolfram|Alpha because I was somewhat confused with the correct interval between two dates.


Try your test cases in other languages. E.G. python3 import datetime ; (datetime.datetime.strptime("2019-01-31","%Y-%m-%d") - datetime.datetime.strptime("2012-02-29","%Y-%m-%d"))

The result, which reminds me of one of the other reasons I avoid python for trivial projects, is the duration / interval answer of days=2528.

Part of the bug is surely in Wolfram|Alpha returning the interval broken out in human durations; but importantly those durations __don't use fixed time values__. The precise length of a //year// and of a //month// are variable.

I still suspect there's double or single counting of leap-days in those durations as the reverse ones clash with converting a duration back to a whole number.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: