Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

80
Views
Temporal js yearMonth.subtract return the same month

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] 
about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

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.

about 4 years ago · Santiago Gelvez Report

0

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] 
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!