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

331
Views
How to format date to mm/dd/yyyy in moment / date-fns?

Here I am receiving the last updated data as an API response like this

let data = {
          "year": 2021,
          "monthValue": 11,
          "dayOfMonth": 9
         }

I have to covert this above object into this format MM/DD/YYYY.

Can convert the above data using string interpolation like this ${data.dayOfMonth}/${data.monthValue}/${data.year} But is there any other way we can use libraries to get this work done.

For e.g in the above string interpolation method, can't able to add 0 at the beginning of the dayOfMonth and monthValue when they are given as single digits.

can we achieve this using moment or date-fns?

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

0

You don't need a library just to zero-pad a 1–2 digit number:

function pad2 (n) {
  return String(n).padStart(2, '0');
}

function format (data) {
  return `${pad2(data.monthValue)}/${pad2(data.dayOfMonth)}/${data.year}`;
}


const data = {
  "year": 2021,
  "monthValue": 11,
  "dayOfMonth": 9,
};

const result = format(data);
console.log(result); // 11/09/2021

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!