I've got a string which represents date. I turn it into date object and count another date one month ahead. Then I want to count the difference in days between them but I don't know why both became the same and the final resault is always zero. How can I do it?
我有一个表示日期的字符串。我将它变成日期对象并提前一个月计算另一个日期。然后我想计算它们之间的天数差异,但我不知道为什么两者都变得相同而最终的冲击总是为零。我该怎么做?
var dateString = '2017-08-03';
var dateFrom = new Date(dateString);
console.log(dateFrom); //Thu Aug 03 2017 02:00:00 GMT+0200 (CEST)
var dateTo = new Date(dateFrom.setMonth(dateFrom.getMonth()+1));
console.log(dateTo); //Sun Sep 03 2017 02:00:00 GMT+0200 (CEST) (one month later)
console.log(dateFrom); //Sun Sep 03 2017 02:00:00 GMT+0200 (CEST)// (two dates turn into the same)
var difference = Math.floor((dateTo - dateFrom) / 86400000); // 0
2 个解决方案
#1
0
that is happening because in var dateTo = new Date(dateFrom.setMonth(dateFrom.getMonth()+1))
you are actually changing dateFrom by setting is month to next month. you can do something like this.
这是因为在var dateTo = new Date(dateFrom.setMonth(dateFrom.getMonth()+ 1))你实际上是通过设置月份到下个月来更改dateFrom。你可以做这样的事情。
var dateString = '2017-08-03'
var dateFrom = new Date(dateString);
console.log(dateFrom); //Thu Aug 03 2017 02:00:00 GMT+0200 (CEST)
var dateTo = new Date(dateString);
dateTo = new Date(dateTo.setMonth(dateTo.getMonth()+1));
console.log(dateTo); //Sun Sep 03 2017 02:00:00 GMT+0200 (CEST) (one month later)
console.log(dateFrom); //Sun Sep 03 2017 02:00:00 GMT+0200 (CEST)// (two dates turn into the same)
var difference = Math.floor((dateTo - dateFrom) / 86400000); // 0
#2
0
but I don't know why both became the same and the final result is always zero.
但我不知道为什么两者都变得相同而且最终结果总是为零。
The Ans is below line
Ans紧随其后
dateFrom.setMonth(dateFrom.getMonth()+1)
You are trying to set Month month of from date and then using this updated date you are creating new date. if you to achieve desire result you may use below script .
您正尝试设置从月开始的月份,然后使用此更新日期创建新日期。如果你想达到欲望的结果,你可以使用下面的脚本。
var dateString = '2017-08-03';
var dateFrom = new Date(dateString);
console.log(dateFrom); //Thu Aug 03 2017 02:00:00 GMT+0200 (CEST)
var dateTo = new Date(dateFrom); //ADDED
dateTo = dateTo.setMonth(dateFrom.getMonth()+1) //Updated
var difference = Math.floor((dateTo - dateFrom) / 86400000);
#1
0
that is happening because in var dateTo = new Date(dateFrom.setMonth(dateFrom.getMonth()+1))
you are actually changing dateFrom by setting is month to next month. you can do something like this.
这是因为在var dateTo = new Date(dateFrom.setMonth(dateFrom.getMonth()+ 1))你实际上是通过设置月份到下个月来更改dateFrom。你可以做这样的事情。
var dateString = '2017-08-03'
var dateFrom = new Date(dateString);
console.log(dateFrom); //Thu Aug 03 2017 02:00:00 GMT+0200 (CEST)
var dateTo = new Date(dateString);
dateTo = new Date(dateTo.setMonth(dateTo.getMonth()+1));
console.log(dateTo); //Sun Sep 03 2017 02:00:00 GMT+0200 (CEST) (one month later)
console.log(dateFrom); //Sun Sep 03 2017 02:00:00 GMT+0200 (CEST)// (two dates turn into the same)
var difference = Math.floor((dateTo - dateFrom) / 86400000); // 0
#2
0
but I don't know why both became the same and the final result is always zero.
但我不知道为什么两者都变得相同而且最终结果总是为零。
The Ans is below line
Ans紧随其后
dateFrom.setMonth(dateFrom.getMonth()+1)
You are trying to set Month month of from date and then using this updated date you are creating new date. if you to achieve desire result you may use below script .
您正尝试设置从月开始的月份,然后使用此更新日期创建新日期。如果你想达到欲望的结果,你可以使用下面的脚本。
var dateString = '2017-08-03';
var dateFrom = new Date(dateString);
console.log(dateFrom); //Thu Aug 03 2017 02:00:00 GMT+0200 (CEST)
var dateTo = new Date(dateFrom); //ADDED
dateTo = dateTo.setMonth(dateFrom.getMonth()+1) //Updated
var difference = Math.floor((dateTo - dateFrom) / 86400000);