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

217
Views
Date from string treated as wrong format

I had a string value = "01/08/2021 - 31/08/2021" and i need to get the starting date using value.substring(0,10) which return as 01/08/2021. But when i converting it to date using new Date(value.substring(0,10)) it convert the date format as 2021-01-08(yyyy-mm-dd).

Any help on this ?

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

0

Split into the year, month and day

const [day, month, year] = value.substring(0,10).split('/');

Use the Date constructor

new Date(year, month - 1, day);
about 4 years ago · Juan Pablo Isaza Report

0

JavaScript date decodes your date string to "mm/dd/yyyy" format. You can simply split your date string to month - mm, date - dd and year - yyyy and provide this to date object.

Please Note: Dont forget to reduce 1 from the month number. Because JavaScript date counts month from 0 to 11.

const value = "01/08/2021 - 31/08/2021";
//  const startDate = value.substring(0,10);
const [startDate, endDate] = value.split(" - ");
const [dd, mm, yyyy] = startDate.split("/");
console.log(startDate);
console.log(new Date(yyyy, +mm - 1, dd).toDateString());

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!