• ricecake@sh.itjust.works
    link
    fedilink
    arrow-up
    2
    ·
    1 year ago

    What behavior do you expect? Specifically.

    I’m arguing that every answer is wrong, and will return bizarre results, be aggravating, useless or some combination therein for some conditions.
    Therefore you have to know the API, because every language will fuck you, and JavaScript isn’t special in this specific case.

    Adding the number of days in the month to the date as “add one month” is just as rational as any other choice.

    • ☆ Yσɠƚԋσʂ ☆@lemmy.mlOP
      link
      fedilink
      arrow-up
      2
      arrow-down
      8
      ·
      1 year ago

      The behavior I would expect would be to throw an error when the date is outside of the range of valid dates for the month. If you try to create a date on February 31st throw an error. Have you seriously never used a good API that’s intuitive and isn’t full of gotchas?

      • ricecake@sh.itjust.works
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

        Have you never done Datetime math before?

        You didn’t run new Date(2019, 02, 31), you asked it for one month from January 31st.
        One month after any given day of the month is, by most people’s intuition, a valid thing to ask for.

        Your solution of making the API throw an exception for every 31st of the year is vastly less intuitive to me than “a month is 31 days in January, and 28 or 29 in February, so adding a month does different things in different months”, because at least for those is can also query how many days are in the month.

        If I say to add four days, will it throw an exception if it’s the 31st? No month has 35 days, so I should get an exception, right?
        Or is it just this weird caveat around months? Does it apply to time?

        • ☆ Yσɠƚԋσʂ ☆@lemmy.mlOP
          link
          fedilink
          arrow-up
          2
          arrow-down
          7
          ·
          edit-2
          1 year ago

          One month after any given day of the month is, by most people’s intuition, a valid thing to ask for.

          Nobody actually thinks this way, when you ask somebody what’s the next month they don’t go, oh today is the x day of the month and I’m going to add days to the current date to see what’s the next month. That’s not how vast majority of people think about this intuitively. Clearly you and people who designed Js Date API do though.

          “a month is 31 days in January, and 28 or 29 in February, so adding a month does different things in different months”

          That’s how months work. Each month has a different number of days.

          If I say to add four days, will it throw an exception if it’s the 31st? No month has 35 days, so I should get an exception, right?

          Yeah, it’s how months work.

          In fact, this isn’t a hypothetical argument this is how date APIs work in sane languages like Java. For example:

          java.time.LocalDate.of(2023, 2, 31)
          > java.time.DateTimeException: Invalid date 'FEBRUARY 31'
          
          java.time.LocalDate.of(2023, 2, 3)
          > #object[java.time.LocalDate 0x2bc77260 "2023-02-03"]
          

          The fact that you think what Js API dies is preferable to this is frankly surreal to me.