使用此函数时出现错误“ start.getDate()不是函数”

时间:2020-11-02 10:47:56

标签: javascript node.js

                const start = body.startDate; 
                const end  =body.endDate;   
                var dayCount = 0

                while (end > start) {
                  dayCount++
                  start.setDate(start.getDate() + 1)
                }
                console.log("datedifferent"+dayCount);

为什么我遇到错误start.getDate()不起作用

2 个答案:

答案 0 :(得分:0)

const start = new Date(Date.parse(body.startDate)); 
const end = new Date(Date.parse(body.endDate));   
var dayCount = 0

while (end > start) {
  dayCount++
  start.setDate(start.getDate() + 1)
}
console.log("datedifferent"+dayCount);

答案 1 :(得分:0)

变量start不是日期对象,因此会出现该错误。

按照Prime用户的答案将开始创建为日期对象。