簡體   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