Date generation for the first day of the next month

Hi all.

How can I generate a date for the first day of the next month from the specified one.

For example, the current date is 20.03.2024 (from the global variable)
The next date is 01.04.2024

For example, the current date is 03.04.2024 (from the global variable)
The next date is 01.05.2024

Hey @berson :wave:

I’m sure there are more efficient ways to do it but given the format you’re using and the output you want to see:

let d = new Date("2024.05.04");
d.setMonth(d.getMonth() + 1, 1);

console.log(d.toLocaleDateString('en-GB').replace(/[\s/]+/g, '.'));

That might work for you.

1 Like

Yes, that’s great.

I want to make a format d.format(YYYY-MM-DD);
but it’s dont’t work

let d = new Date("2024.05.04");
d.setMonth(d.getMonth() + 1, 1);

console.log(d.format(YYYY-MM-DD);

add: 14_47

It’s work!

let d = new Date("2024.05.04");
d.setMonth(d.getMonth() + 1, 1);

console.log(d.toISOString().substring(0, 10));
1 Like

Cool, are you all set now? :pray:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.