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

299
Views
Get year and month formatted in JavaScript

I need to get the year and month in JavaScript in a specific format, but cannot figure out how. I need it like this:

2022-03

I've tried using these functions but I need the 0 in the month:

console.log(new Date().getFullYear())
console.log(new Date().getMonth() + 1)

but the result is 2022 and 3

How can I format dates in javascript?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can do something like this, append leading zero to date and slice last two digits

let d = new Date();
date = [
  d.getFullYear(),
  ('0' + (d.getMonth() + 1)).slice(-2)
].join('-');

console.log(date)

about 4 years ago · Juan Pablo Isaza 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!