使用此代码我得到不包括开始日期的日期
var startdate = new Date("");
var enddate = new Date("");
while (startdate < enddate) {
startdate.setDate(startdate.getDate() + 1);
dates.push(new Date(startdate).format("mm/dd/yyyy"));
}
最佳答案
您应该将 new Date(startdate)
替换为 new Date(startdate.getTime())
以避免 JS 错误。但是,你不能简单地用format()
格式化JS Date对象,因为这个函数不存在,你必须自己提取年月日。如果要这样做,请注意月份值是从零开始的。
请考虑改用像 Moment.js 这样的库,它可以方便地操作日期、时间和持续时间。
关于javascript - 我需要在 JavaScript 中获取任意两个日期之间的所有日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58059481/