Temporal js yearMonth.subtract return同月

Temporal js yearMonth.subtract return the same month

我正在尝试使用 Temporal yearMonth.subtract 但没有用。 add 正在按预期工作。

演示:https://codesandbox.io/s/objective-knuth-20ov2?file=/src/index.js

import { Temporal } from "@js-temporal/polyfill";

const currentMonth = Temporal.Now.plainDate("gregory").toPlainYearMonth();

console.log("currentMonth", currentMonth.toString());
console.log("after", currentMonth.add({ months: 1 }).toString());
console.log("before", currentMonth.subtract({ months: 1 }).toString());
currentMonth 2021-12-01[u-ca=gregory] 
after 2022-01-01[u-ca=gregory] 
before 2021-12-01[u-ca=gregory] 

在 addition/subtraction 操作之前将 Temporal toPlainYearMonth 方法应用于 currentMonth 导致了问题。

这按预期工作:

const currentMonth = Temporal.Now.plainDate("gregory");

console.log("currentMonth", currentMonth.toPlainYearMonth().toString());
console.log("after", currentMonth.add({ months: 1 }).toPlainYearMonth().toString());
console.log("before", currentMonth.subtract({ months: 1 }).toPlainYearMonth().toString());

控制台输出:

currentMonth 2021-12-01[u-ca=gregory] 
after 2022-01-01[u-ca=gregory] 
before 2021-11-01[u-ca=gregory] 

OP 中的代码应该可以按预期工作,这是该 polyfill v0.2.0 中的错误。好像会在即将发布的 v0.3.0 中修复。