首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >时态js yearMonth.subtract同月返回

时态js yearMonth.subtract同月返回
EN

Stack Overflow用户
提问于 2021-12-10 17:48:31
回答 2查看 124关注 0票数 0

我正在尝试使用时态yearMonth.subtract,但不起作用。add正在按预期工作。

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

代码语言:javascript
复制
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());
代码语言:javascript
复制
currentMonth 2021-12-01[u-ca=gregory] 
after 2022-01-01[u-ca=gregory] 
before 2021-12-01[u-ca=gregory] 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-12-10 20:59:21

OP中的代码应该像您所预期的那样工作,这是一个错误,在0.2.0版本的这个多边形填充。它似乎将在不久即将到来的0.3.0版本中得到修正。

票数 3
EN

Stack Overflow用户

发布于 2021-12-10 19:01:49

在加/减操作之前将时间 toPlainYearMonth方法应用于currentMonth是造成问题的原因。

这项工作如预期的那样:

代码语言:javascript
复制
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());

控制台输出:

代码语言:javascript
复制
currentMonth 2021-12-01[u-ca=gregory] 
after 2022-01-01[u-ca=gregory] 
before 2021-11-01[u-ca=gregory] 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70308523

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档