Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda