我正在嘗試使用 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]
uj5u.com熱心網友回復:
OP 中的代碼應該按您的預期作業,這是該 polyfill v0.2.0 中的一個錯誤。似乎它將在即將推出的 v0.3.0 中修復。
uj5u.com熱心網友回復:
在加法/減法運算之前應用時間 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://www.uj5u.com/gongcheng/383589.html
標籤:javascript 日期 颞
上一篇:字串到日期的轉換有時會使用DateFormatter回傳nil
下一篇:變數的頻率(按月)
