Home > Blockchain >  Temporal js yearMonth.subtract return the same month
Temporal js yearMonth.subtract return the same month

Time:12-11

I am trying to use Temporal yearMonth.subtract but is not working. add is working as expected.

Demo: 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] 

CodePudding user response:

Applying the Temporal toPlainYearMonth method to currentMonth before the addition/subtraction operation is causing the problem.

This works as expected:

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());

Console output:

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

CodePudding user response:

The code in the OP ought to work as you expected, this is a bug in v0.2.0 of that polyfill. It seems like it will be fixed in the shortly upcoming v0.3.0.

  • Related