gpt4 book ai didi

Javascript 日期比较奇怪的行为

转载 作者:行者123 更新时间:2023-12-03 07:23:48 26 4
gpt4 key购买 nike

我有一个计算开始日期和结束日期的函数。

然后打印出以下代码:

   $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

我在这里做错了什么吗?

最佳答案

我没有关于你的变量的完整上下文,但我创建了一个 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);

关于Javascript 日期比较奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36091505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com