对于给定的日期,我需要返回一个数组,其中包含当月剩余的每一天的日期对象。
我需要一个for
循环创建new Date()
对象设置为该月剩余的每一天,将它们添加到数组并返回该数组。
我想出了代码来检索该月的剩余天数,但是由于某种原因我无法弄清楚循环/数组部分。
这是我的代码:
function findDays(theDate) {
var now = new Date();
var date = now.getDate();
var last = new Date(now.getFullYear(), now.getMonth()+1, 0).getDate();
return last - date;
}
console.log(findDays() - (new Date(2016, 0, 1)).getDate());
您可以使用getTime
and setTime
Date 对象的方法一次前进一天。
var days = [];
var now = new Date();
var day = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0, 0);
while (day.getMonth() === now.getMonth()) {
days.push(day);
// Go to the next day
day = new Date(day.getTime() + 86400000); // There are 86400000 milliseconds in a day
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)