Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!