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

174
Views
Javascript Insert Date without leading zeros

I am absolutely new to Javascript. I am trying to get a certain format for a date I need inserted. Essentially, I want it to look like mm/dd/yy but I do not want leading zeros when there is a single digit month or date. Is this possible? I tried the code here but it gives me leading zeros and a four digit year.

Thank you all in advance.

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

0

Try this, it does what you want:

let today = new Date();
const day = String(today.getDate())
const month = String(today.getMonth() + 1); // January is counted as 0
let year = today.getFullYear();

year = `${year}`.split("").splice(2,3).join("")

today = `${day}/${month}/${year}`;
document.write(today);

about 4 years ago · Juan Pablo Isaza Report

0

I think you are looking at the top votes answer from the question you are looking at.

In the answer, they use the padstart() to insert 0 for single day.

The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start of the current string.

The simplest solution is to just remove the two padStart from that answer.

var today = new Date();
var dd = String(today.getDate())
var mm = String(today.getMonth() + 1); //January is 0!
var yyyy = today.getFullYear();

today = mm + '/' + dd + '/' + yyyy;
document.write(today);

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!