if 语句的条件应该等于 true,但事实并非如此
Condition of if statement should equal true but it doesn't
if (emptyGrid) {
ranges.push({ start: new Date(IndividualBugetFigures.awardYears_Unformatted[elementIndex].Begin), end: new Date(IndividualBugetFigures.awardYears_Unformatted[elementIndex].End) })
}
else {
for (var i = 1; i < thisGridData.length; i++) {
//debugger;
if (i === 1 && new Date(thisGridData[i].childEffBegDate) > new Date(IndividualBugetFigures.awardYears_Unformatted[elementIndex].Begin)) {
ranges.push({ start: new Date(IndividualBugetFigures.awardYears_Unformatted[elementIndex].Begin), end: new Date(moment(thisGridData[i].childEffBegDate).subtract(1, 'days').calendar()) });
}
else {
if (i + 1 < thisGridData.length) {
//console.log('beginDate EndDate', new Date(thisGridData[i].childEffEndDate) + ' ' + new Date(thisGridData[i + 1].childEffBegDate));
console.log('in the and part', new Date(moment(thisGridData[i].childEffEndDate).add(1, 'days').calendar()) + ' ' + new Date(thisGridData[i + 1].childEffBegDate));
//debugger;
if (new Date(moment(thisGridData[i].childEffEndDate).add(1, 'days').calendar()) === new Date(thisGridData[i + 1].childEffBegDate)) {
if (i + 1 === thisGridData.length -1) {
return false;
}
}
else if (new Date(thisGridData[i].childEffEndDate) < new Date(thisGridData[i + 1].childEffBegDate) && new Date(moment(thisGridData[i].childEffEndDate).add(1, 'days').calendar()) !== new Date(thisGridData[i + 1].childEffBegDate))
ranges.push({ start: new Date(moment(thisGridData[i].childEffEndDate).add(1, 'days').calendar()), end: new Date(moment(thisGridData[i + 1].childEffBegDate).subtract(1, 'days').calendar()) });
}
if (i === thisGridData.length - 1 && new Date(thisGridData[i].childEffEndDate) < new Date(IndividualBugetFigures.awardYears_Unformatted[elementIndex].End))
ranges.push({ start: new Date(moment(thisGridData[i].childEffEndDate).add(1, 'days').calendar()), end: new Date(IndividualBugetFigures.awardYears_Unformatted[elementIndex].End) });
}
}
}
上面的代码按应有的方式遍历 if 语句,除非它到达此部分:
if (new Date(moment(thisGridData[i].childEffEndDate).add(1, 'days').calendar()) === new Date(thisGridData[i + 1].childEffBegDate)) {
if (i + 1 === thisGridData.length -1) {
return false;
}
}
当我安慰时:
console.log('in the and part', new Date(moment(thisGridData[i].childEffEndDate).add(1, 'days').calendar()) + ' ' + new Date(thisGridData[i + 1].childEffBegDate));
我得到——
in the and part Fri Jan 02 2015 00:00:00 GMT-0600 (Central Standard Time) Fri Jan 02 2015 00:00:00 GMT-0600 (Central Standard Time)
那么,为什么 if 语句的计算结果不为 true呢? 当我单步执行代码时,它会到达这一行,然后转到下一个 if 。 提前感谢您的有用评论。
两个对象永远不会相同,即使在严格比较日期对象时也是如此。
改为使用getTime()
比较纪元中的毫秒数
if (new Date(moment(thisGridData[i].childEffEndDate).add(1, 'days').calendar()).getTime() === new Date(thisGridData[i + 1].childEffBegDate).getTime()) {...
您正在将日期实例与===
进行比较。两个单独的 Date 实例,即使它们引用完全相同的时间点,也永远不会比较为相等,因为它们是对象。
您可以使用一元+
操作数将 Date 实例强制为数字,并改为比较基础时间戳。
if (+new Date(moment(thisGridData[i].childEffEndDate).add(1, 'days').calendar()) === +new Date(thisGridData[i + 1].childEffBegDate)) {
此外,您应该能够以相同的方式使用 Momentjs 对象,并节省 Date 实例的构造。
您需要了解比较对象和某些特定值之间存在差异。 :)
这是我在谷歌搜索"比较日期值javascript"时发现的第一个结果(它是不同日期比较的摘要,如<,>,=,还有一个解释为什么它以前对你不起作用):
使用 JavaScript 比较两个日期
相关文章:
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- AngularJS-使用'true'属性
- JavaScript-切换“;全部检查”;复选框true/false
- location.reload(true)崩溃浏览器选项卡
- 指令的模板必须只有一个根元素:With restrict E&替换true
- JavaScript:只有当数组中的所有项都为true时才执行函数
- 对于loop.if-仅在经过所有间隔后返回true
- if(foo!==null)的计算结果为true,即使foo为null
- ngIf的计算结果始终为true
- 如果函数返回True,则显示Javascript按钮
- (false==false==true)怎么可能是真的
- Javascript If else 只返回 TRUE 或只返回 FALSE
- sails.js beforeCreate方法只接收required设置为true的模型属性
- 在$state.go之后执行$window.location.reload(true)
- 尝试简化检查对象键是否为true并将其推送到另一个对象
- 过滤器返回true或false
- 什么是'true'和'false'和'null'
- 如何使用选项useCSS set true为bxSlider加载onSlideAfter()事件
- 当我的条件为true时,显示一条弹出消息
- if 语句的条件应该等于 true,但事实并非如此