繁体   English   中英

JavaScript如何获取下个月的第一天

[英]How to get the first day of the next month in JavaScript

我需要获取下个月的第一天以获取 dd/mm/yyyy 格式的 datePicker。

我该怎么做?

我有这个代码:

 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)

但它给了这个:

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

我需要它01/01/2022

这里有什么帮助=)?

要获得下个月的第一天:

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

然后格式化为 dd/mm/yyyy:

d.toLocaleDateString('en-GB');

作为一个可运行的片段:

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

您可以使用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.

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