[英]get number of days in the CURRENT month using javascript
对于我的网站,我试图获取某个功能当前月份的天数。
我在网上看到过获取指定月份天数的示例,但是我需要获取当前月份的天数并找出该月还剩多少天。
这是我设法放在一起的代码:
function myFunction() {
var today = new Date();
var month = today.getMonth();
console.log(month);
}
myFunction();
这是你想要的吗?
function daysInThisMonth() {
var now = new Date();
return new Date(now.getFullYear(), now.getMonth()+1, 0).getDate();
}
基于这篇文章的答案: 使用 javascript 确定一个月中的天数的最佳方法是什么?
修改它以适用于本月应该很容易 这是您的代码和另一篇文章中的函数:
function myFunction() {
var today = new Date();
var month = today.getMonth();
console.log(daysInMonth(month + 1, today.getFullYear()))
}
function daysInMonth(month,year) {
return new Date(year, month, 0).getDate();
}
myFunction();
请注意,函数date.getMonth()
返回一个从零开始的数字,因此只需加 1 即可标准化。
var numberOfDaysOnMonth = function() {
let h = new Date()
h.setMonth(h.getMonth() + 1)
h.setDate(h.getDate() - 1)
return h.getDate()
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.