我正在尝试使用时态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] 发布于 2021-12-10 20:59:21
OP中的代码应该像您所预期的那样工作,这是一个错误,在0.2.0版本的这个多边形填充。它似乎将在不久即将到来的0.3.0版本中得到修正。
发布于 2021-12-10 19:01:49
在加/减操作之前将时间 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] https://stackoverflow.com/questions/70308523
复制相似问题