我有一个计算开始日期和结束日期的函数。
然后打印出这段代码:
$log.log("currentDate: " + currentDate);
$log.log("currentDate: " + startDate + " " + (currentDate.valueOf() > startDate.valueOf()));
$log.log("currentDate: " + endDate + " " + (currentDate.valueOf() < endDate.valueOf()));
但是在我的控制台窗口中,比较返回false,即使它们应该返回true!
currentDate: Fri Mar 18 2016 14:09:26 GMT-0500 (CDT)angular.js:12722
currentDate: Fri Mar 18 2016 08:00:26 GMT-0500 (CDT) false
currentDate: Fri Mar 18 2016 19:30:26 GMT-0500 (CDT) false
我在这里做错了吗?
答案 0 :(得分:1)
我没有关于你的变量的完整上下文,但我创建了一个plunker并且仅使用setTimeout按预期工作。请检查valueOf()如何按预期工作并提供 true
var currentDate, endDate, startDate= new Date();
setTimeout(function(){
currentDate = new Date();
}, 1000)
setTimeout(function(){
endDate = new Date();
},2000)
setTimeout(function(){
$log.log("currentDate: " + currentDate);
$log.log("currentDate: " + startDate + " " + (currentDate.valueOf() > startDate.valueOf()));
$log.log("currentDate: " + endDate + " " + (currentDate.valueOf() < endDate.valueOf()));
$scope.message = 'Hello World!';
},3000);