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

143
Views
react-datepicker doesn't setState to dd/MM/yy

The date is presented in the right format (31/08/21), but onChange sets the startDate to something like this:

Tue Aug 31 2021 21:29:17 GMT+0200 (Mitteleuropäische Sommerzeit)

How can I save the format I display?

import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";

export default function Bodyweight() {
  
const [startDate, setStartDate] = useState(new Date());

return(
<div>
  <DatePicker
          dateFormat="dd/MM/yy"
          selected={startDate}
          onChange={(date) => setStartDate(date)}
        />
</div>
)}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Your DatePicker component accepts date in Tue Aug 31 2021 21:29:17 GMT+0200 (Mitteleuropäische Sommerzeit) format.

And it also returns the date in same format.

In order to convert the date in required format, you have few options.

  1. If DatePicker is your own component, check its implementation and modify the code, so that it returns the date in required format.

  2. If option 1 is not an option, then manually convert the date in required format. Note that you may need a third variable to store the formatted date, as your DatePicker components accepts an actual date object not string.

Refer this link to convert date in req format. Format JavaScript date as yyyy-mm-dd

about 4 years ago · Juan Pablo Isaza Report

0

Just use new Date().toLocaleDateString() in your initial state set, i.e,

const [startDate, setStartDate] = useState(new Date().toLocaleDateString());

I'm answering according to my understanding, if this is not the solution you are looking for, please do rectify in comments

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!