[英]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.我需要获取下个月的第一天以获取 dd/mm/yyyy 格式的 datePicker。
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
我需要它
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:然后格式化为 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您可以使用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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.