简体   繁体   中英

How to get the first day of the next month in JavaScript

I need to get the first day of the next Month for a datePicker in dd/mm/yyyy format.

how can i do it?

i have this code:

 var now = new Date(); if (now.getMonth() == 11) { var current = new Date(now.getFullYear() + 1, 0, 1); } else { var current = new Date(now.getFullYear(), now.getMonth() + 1, 1); } console.log(current)

but it's give this:

Sat Jan 01 2022 00:00:00 GMT+0200 

i need it like this 01/01/2022

any help here =)?

To get the first of next month:

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

Then to format as dd/mm/yyyy:

d.toLocaleDateString('en-GB');

As a runnable snippet:

 let d = new Date(); d.setMonth(d.getMonth() + 1, 1); console.log(d.toLocaleDateString('en-GB'));

You can use toLocaleDateString

 var now = new Date(); var current; if (now.getMonth() == 11) { current = new Date(now.getFullYear() + 1, 0, 1); } else { current = new Date(now.getFullYear(), now.getMonth() + 1, 1); } console.log(current.toLocaleDateString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM