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

136
Vistas
Javascript Date Object returning incorrect first day of the month

I'm using the following code in a React app to get the first day of the month (to set defaults for a date picker component).

let date = new Date();
let firstDay = new Date(date.getFullYear(), date.getMonth(), 1).toISOString().slice(0, 10);

For some reason today, on 1st April, it's returning 31st March. I've tested in Chrome console and various other online JS runtime tools and some return 31-03-2022 and some return 01-04-2022. Is there something I'm missing?

Edit:

My main issue was I needed a YYYY-MM-DD format in the user's local time zone. Using .toISOString provides UTC time zone and I'm unable to find a solution which circumvents this. I'm using the following workaround (helped by Mac's answer):

 let date = new Date();
 let firstDayCopy = String(date.getDate()).padStart(2, '0') + '-' + String(date.getMonth() + 1).padStart(2, '0') + '-' + date.getFullYear();
 let firstDay = firstDayCopy.split("-").reverse().join("-");

Edit 2:

Or of course, just reverse the order to get:

let firstDay = String(date.getFullYear() + '-' + String(date.getMonth() + 1).padStart(2, '0') + '-' + String(date.getDate()).padStart(2, '0'))
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Try this:

let date = new Date();
let firstDay = date.getDate() + '-'
 + (date.getMonth()+ 1) + '-' + date.getFullYear();

or this, if You want to have the result format with '0':

let date = new Date();
let firstDay = String(date.getDate()).padStart(2, '0') + '-'
 + String(date.getMonth()+ 1).padStart(2, '0') + '-' + date.getFullYear();
about 4 years ago · Juan Pablo Isaza Denunciar

0

As robertklep stated, timezone plays a large roll in dates.

The problem with your result is being masked by the fact you are slicing the date part of the ISOString. You'll likely see the full string as:

"2022-03-31T23:00:00.000Z".

Notice the time is 1 hour earlier than local time? This is in line with UTC. and is due to my current timezone that is offset by 1 hour.

If you're not concerned about timezone, simply use: .toLocaleDateString();

let date = new Date();
let firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
console.log("ISO-8601", firstDay.toISOString());
console.log("Local String:", firstDay.toLocaleDateString("en-GB"));

you can also use .toLocaleDateString("en-US") depending on format

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