Estoy tratando de generar la fecha actual en el formato Date Month, Year . Actualmente estoy usando el siguiente código:
function currentDate() { var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]; var date = new Date(); var d = date.getDate(); var m = date.getMonth(); var y = date.getFullYear(); return `${d} ${months[m]}, ${y}`; }Aunque, funciona perfectamente. Pero, ¿hay una manera más corta de hacer esto...?
Algo como:
(new Date()).format('d M, Y')Desafortunadamente, no hay.
Pero puedes usar bibliotecas como luxon :
DateTime.now().toFormat('d M, Y') O, si desea permanecer nativo, puede usar Intl.DateTimeFormat :
new Intl.DateTimeFormat('es-US', { dateStyle: 'full' }).format(d); Sin embargo, ECMAScript actualmente tiene una propuesta para mejorar la clase Date agregando un objeto global Temporal :
Temporal.now.date().toLocaleString('es-US', { weekday: 'long', });