JavaScript - calculated first day of month changes to last in ISO standard

3 points
Asked by:

How can I calculate the first day of the month in ISO standard?

I've used the following solution but when I try to convert it to ISO standard I get the last day of month.

// ONLINE-RUNNER:browser;

let date = new Date('Mon Aug 02 2021');
let firstDay = new Date(date.getFullYear(), date.getMonth(), 1);

console.log(firstDay); // Sun Aug 01 2021
console.log(firstDay.toISOString()); // 2021-07-31T22:00:00.000Z

I want to receive the following output:

1 answer
9 points
Answered by:

toISOString() method returns always time as ISO 8601, where the timezone is always set to zero offset according to global time what is denoted by the Z suffix.

Your local time has 2 hours different than global time.

date variable contains 2021-08-01 00:00 in your local time (GMT+0200)

firstDay variable contains 2021-07-31 22:00 in global time (offset zero)

0 comments Add comment
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

â€ïžđŸ’» 🙂