Standards for Date/Time addition?

前端 未结 8 1114
自闭症患者
自闭症患者 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:31

    First day of the month + 1 month should equal the first of the next month. Trying this on SQL Server

              SELECT CAST ('01/01/2012' AS DateTime), DATEADD (m, 1, '01/01/2012')
    UNION ALL SELECT CAST ('02/01/2012' AS DateTime), DATEADD (m, 1, '02/01/2012')
    UNION ALL SELECT CAST ('03/01/2012' AS DateTime), DATEADD (m, 1, '03/01/2012')
    UNION ALL SELECT CAST ('04/01/2012' AS DateTime), DATEADD (m, 1, '04/01/2012')
    UNION ALL SELECT CAST ('05/01/2012' AS DateTime), DATEADD (m, 1, '05/01/2012')
    

    This results in

    ----------------------- -----------------------
    2012-01-01              2012-02-01             
    2012-02-01              2012-03-01             
    2012-03-01              2012-04-01             
    2012-04-01              2012-05-01             
    2012-05-01              2012-06-01             
    

    Last day of this month + 1 month should equal last day of next month. This should go for next month, current month, 10 months down, etc.

              SELECT CAST ('01/31/2012' AS DateTime), DATEADD (m, 1, '01/31/2012')
    UNION ALL SELECT CAST ('01/30/2012' AS DateTime), DATEADD (m, 1, '01/30/2012')
    UNION ALL SELECT CAST ('01/29/2012' AS DateTime), DATEADD (m, 1, '01/29/2012')
    UNION ALL SELECT CAST ('01/28/2012' AS DateTime), DATEADD (m, 1, '01/28/2012')
    UNION ALL SELECT CAST ('01/27/2012' AS DateTime), DATEADD (m, 1, '01/27/2012')
    UNION ALL SELECT CAST ('01/26/2012' AS DateTime), DATEADD (m, 1, '01/26/2012')
    

    This results in

    ----------------------- -----------------------
    2012-01-31              2012-02-29             
    2012-01-30              2012-02-29             
    2012-01-29              2012-02-29             
    2012-01-28              2012-02-28             
    2012-01-27              2012-02-27             
    2012-01-26              2012-02-26             
    

    See how 31, 30, 29 all become feb 29 (2012 is a leap year).

    p.s. I took off the time parts (all zeroes) to help make it more readable

提交回复
热议问题