2

Estoy bajando un json desde axios en react, y necesito obtener el mes de todas las fechas que baje, sin embargo sale "valor.fecha.getMonth is not a function". Hay alguna forma de alojar la fecha en una variable (algo complicado, ya que se está iterando)?

<tbody>
                    {dailyList.map((valor, key) => (
                        <tr key={key}>
                            <td>
                                <span>{valor.fecha}</span>
                            </td>
                                <span>{valor.cant}</span>
                            </td>
                            <td>
                                <span>{valor.tiempo}</span>
                            </td>
                            <td>
                                <span>{(60/valor.tiempo)}</span>
                            </td>
                            <td>
                                <span>${(1/(60/valor.tiempo)).toFixed(1)}</span>
                            </td>
                            <td>
                                <span>${(1/(60/valor.tiempo)).toFixed(2)*valor.cant}</span>
                            </td>
                            <td>

                            </td>
                            <td>
                                <span>{(valor.fecha).getMonth()}</span>
                            </td>
                        </tr>
                    ))}
                </tbody>
3
  • ¿Podrías editar la pregunta y agregar tu JSON que estás mapeando?
    – Daniel
    el 9 dic. 2021 a las 20:05
  • 1
    ¿Por qué estás accediendo a la función así (valor.fecha).getMonth()? ¿No debería ser así valor.fecha.getMonth()? No entiendo el propósito de esos paréntesis. el 9 dic. 2021 a las 21:28
  • @Daniel el json que obtengo es éste[{"id":2,"fecha":"2022-07-01T00:00:00","maquina":"H2","pieza":"Cortical","cant":12,"tiempo":4},{"id":4,"fecha":"2022-07-02T00:00:00","maquina":"H1","pieza":"05","cant":14,"tiempo":4}] el 10 dic. 2021 a las 11:09

1 respuesta 1

0

Viendo tu JSON que compartes puedes usar toLocaleString

var date =new Date("2022-07-02T00:00:00");
    
console.log(date.toLocaleString('en-US', {year: 'numeric', month: '2-digit', day: '2-digit'}));

que te daria como resultado

07/02/2022

y solo usando localestring

 var date =new Date("2022-07-02T00:00:00");

    
console.log(date.toLocaleString());

tendrias como resultado

7/2/2022, 12:00:00 AM

resolviendo tu error usando tolocalestring

new Date(valor.fecha).toLocaleString()

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.