Tengo un HTML <input type="date" name="departing" /> que devuelve, por ejemplo, la fecha en el siguiente formato: 2021-11-07
Cuando reenvío esta variable a mi manillar, se muestra de la siguiente manera:
Mon Nov 08 2021 01:00:00 GMT+0100 (Central European Standard Time)Me gustaría que se muestre como:
07/11/2021Intenté formatearlo con date-fns así:
departing = format(parseISO(departing.getDate()), "dd/MM/yyyy"); Pero luego mi manillar muestra: "Invalid Date"
Estoy muy confundido. ¿Alguna idea sobre cómo hacer que esa fecha se muestre en el formato 11/07/2021?
En primer lugar, cree un objeto de fecha.
const newDate = new Date();Ahora puedes manipular el código para obtener la respuesta que quieras.
var outputDate = newDate .getDate() + "/" + (newDate.getMonth()+1) + "/" + newDate.getFullYear();La salida se verá como el 11/01/2021
Bueno, podrías ir tan simple como usar los métodos #getDate(), #getMonth() y #getFullYear() por separado y luego mostrarlos en el orden que te gustaría:
const date = new Date(departing); const day = date.getDate(); const month = date.getMonth() + 1; const year = date.getFullYear(); console.log(`${day}/${month}/${year}`);Espero que responda la pregunta.
Podrías hacerlo de varias formas
Método 1:-
let n = new Date() console.log(n.toLocaleDateString("en-US") output=== 31/9/2021método 2: obtener valores separados y combinar
let n = new Date() console.log(n.getDate()+"/"+n.getMonth()+"/"+n.getFullYear()) output=== 31/9/2021