Standards for Date/Time addition?

前端 未结 8 1113
自闭症患者
自闭症患者 2021-02-02 15:40

I\'m looking for standards for Date/Time addition. I haven\'t been able to find any. In particular I\'m hoping to find a spec that defines what should happen when you add a mont

8条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-02 16:13

    Query:

    SELECT
    ADDDATE(DATE('2010-12-31'), INTERVAL 1 MONTH) 'Dec + Month',
    ADDDATE(DATE('2011-01-31'), INTERVAL 1 MONTH) 'Jan + Month',
    ADDDATE(DATE('2011-02-28'), INTERVAL 1 MONTH) 'Feb + Month',
    ADDDATE(DATE('2011-03-31'), INTERVAL 1 MONTH) 'Mar + Month';
    

    Output:

        Dec + Month  Jan + Month  Feb + Month   Mar + Month
        2011-01-31   2011-02-28   2011-03-28    2011-04-30
    

    My conclusion:

    1. Calculate the number of days in the month of the input date.
    2. Add that many days to the input date.
    3. Check if the day in the resulting date exceeds the maximun number of days in the resulting month.
    4. If yes, then change the resulting day to maximum day of the resulting month.

    If you add MONTH, YEAR_MONTH, or YEAR and the resulting date has a day that is larger than the maximum day for the new month, the day is adjusted to the maximum days in the new month

    source

    Problem here is that it doesn't mention that the month is actually the month from the input date.

提交回复
热议问题