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

125
Vistas
cómo obtener la primera fecha y la última fecha del mes y el año en javascript

¿Alguien puede ayudarme a encontrar la primera fecha y la última fecha del mes y el año? Tengo 2 parámetros mes y año, por ejemplo mes = 10 y año = 2021

Intenté un código pero no tiene la respuesta.

 var prevMonthLastDay = new Date(2021, 10, 0); var thisMonthFirstDay = new Date(2021, 10, 1); console.log(prevMonthLastDay); console.log(thisMonthFirstDay);

El código anterior está dando el siguiente resultado:

 2021-10-30T17:00:00.000Z 2021-10-31T17:00:00.000Z

Mi respuesta esperada es la primera fecha 2021-10-01 y la última fecha 2021-10-31

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Puedes probar de esta manera

 var date = new Date(); var firstDay = new Date(date.getFullYear(), date.getMonth(), 1); var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Intente lo siguiente para obtener el resultado esperado:

 function GetFirstAndLastDate(year, month) { var firstDayOfMonth = new Date(year, month-1, 2); var lastDayOfMonth = new Date(year, month, 1); console.log('First Day: ' + firstDayOfMonth.toISOString().substring(0, 10)); console.log('Last Day: ' + lastDayOfMonth.toISOString().substring(0, 10)); } GetFirstAndLastDate(2021, 10);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Estoy un poco confundido con lo que realmente quiere obtener haciendo referencia a sus nombres de variables, así que los incluyo todos:

 const currentYear = 2021; const currentMonth = 10; const prevMonthFirstDay = new Date(currentYear, currentMonth - 2, 1); const prevMonthLastDay = new Date(currentYear, currentMonth - 1, 0); const currentMonthFirstDay = new Date(currentYear, currentMonth - 1, 1); const currentMonthLastDay = new Date(currentYear, currentMonth, 0); const nextMonthFirstDay = new Date(currentYear, currentMonth, 1); const nextMonthLastDay = new Date(currentYear, currentMonth + 1, 0); console.log('prev month first day:', prevMonthFirstDay); console.log('prev month last day:', prevMonthLastDay); console.log('current month first day:', currentMonthFirstDay); console.log('current month last day:', currentMonthLastDay); console.log('next month first day:', nextMonthFirstDay); console.log('next month last day:', nextMonthLastDay);

en mi sistema esto dará:

 prev month first day: Wed Sep 01 2021 00:00:00 GMT+0700 (Western Indonesia Time) prev month last day: Thu Sep 30 2021 00:00:00 GMT+0700 (Western Indonesia Time) current month first day: Fri Oct 01 2021 00:00:00 GMT+0700 (Western Indonesia Time) current month last day: Sun Oct 31 2021 00:00:00 GMT+0700 (Western Indonesia Time) next month first day: Mon Nov 01 2021 00:00:00 GMT+0700 (Western Indonesia Time) next month last day: Tue Nov 30 2021 00:00:00 GMT+0700 (Western Indonesia Time)

así que ahora solo será cuestión de formatear:

 console.log(currentMonthFirstDay.toLocaleDateString('en-GB').split('/').reverse().join('-'));

que te dará:

 2021-10-01
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