Date class setMonth 设置奇怪的值

Date class setMonth sets weird values

我有以下测试代码:

    var d1 : Date = new Date("2016/02/20 15:00:00 UTC-0000");
    trace(d1.toUTCString());
    d1.monthUTC++;
    trace(d1.toUTCString());

    var d2 : Date = new Date("2016/03/31 15:00:00 UTC-0000");
    trace(d2.toUTCString());
    d2.monthUTC++;
    trace(d2.toUTCString());

这个痕迹

[trace] Sat Feb 20 15:00:00 2016 UTC
[trace] Sun Mar 20 15:00:00 2016 UTC
[trace] Thu Mar 31 15:00:00 2016 UTC
[trace] Sun May 1 15:00:00 2016 UTC

为什么第二个例子中的日期跳了1个月零1天而不是一个月? (从3月31日到5月1日)?

我认为解决方案在于日期 class 的内部工作方式:

当我将日期设置为该月的 31 日时,例如:
"2016/03/31 15:00:00 UTC-0000"
然后我将月份增加一个,在内部它变成:
"2016/04/31 15:00:00 UTC-0000"
然后这又在内部解决了。但是由于 4 月只有 30 天,日期溢出到 5 月:
"2016/05/01 15:00:00 UTC-0000"

如果我尝试将 date 字段设置为 4 月 31 日,也会发生同样的事情。

这也意味着官方的ActionScript文档是错误的:

setUTCMonth()
Sets the month, and optionally the day, in universal time(UTC) and returns the new time in milliseconds. Calling this method does not modify the other fields, but the Date.getUTCDay() and Date.getDay() methods might report a new value if the day of the week changes as a result of calling this method.

他们没有想到这种极端情况,当设置月份时也会改变日期。

同样的事情也发生在 JavaScript。