javascript 时刻 .isAfter 没有正确返回

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30652365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:29:41  来源:igfitidea点击:

Moment .isAfter not returning correctly

javascriptmomentjs

提问by Mdd

I have a string that is stored in UTC time. I am trying to see if this time is after the current UTC time. I am using momentjs and the isAfter() method returns the incorrect value when there is only 1 hour difference.

我有一个以 UTC 时间存储的字符串。我想看看这个时间是否在当前 UTC 时间之后。我正在使用 momentjs 并且 isAfter() 方法在只有 1 小时的差异时返回不正确的值。

The active_time variable happens at 15:00 utc. The current_time is set to 16:00 utc. So I think active_time.isAfter(current_time)should return falsebut it is returning true. How can I make it return false?

active_time 变量发生在 15:00 UTC。current_time 设置为 16:00 UTC。所以我认为active_time.isAfter(current_time)应该返回false但它正在返回true。我怎样才能让它回来false

jsFiddle link: http://jsfiddle.net/Ln1bz1nx/

jsFiddle 链接:http: //jsfiddle.net/Ln1bz1nx/

Code:

代码:

//String is already in utc time
var active_time = moment('2015-06-04T15:00Z', 'YYYY-MM-DD[T]HH:mm[Z]');

//Convert current time to moment object with utc time
var current_time = moment( moment('2015-06-04T16:00Z').utc().format('YYYY-MM-DD[T]HH:mm[Z]') ); 

console.log('active_time =',active_time);
console.log('current_time =',current_time);
console.log( active_time.isAfter(current_time) ); //Why does this return true?

回答by Alexander Lindsay

Even though the first date string is utc, you still need to put the moment into utc mode before you compare. Take a look at the docs here: http://momentjs.com/docs/#/parsing/utc/

即使第一个日期字符串是 utc,您仍然需要在比较之前将时刻置于 utc 模式。看看这里的文档:http: //momentjs.com/docs/#/parsing/utc/

//String is already in utc time, but still need to put it into utc mode
var active_time = moment.utc('2015-06-04T15:00Z', 'YYYY-MM-DD[T]HH:mm[Z]');

//Convert current time to moment object with utc time
var current_time = moment.utc('2015-06-04T16:00Z', 'YYYY-MM-DD[T]HH:mm[Z]');

console.log('active_time =',active_time.format());
console.log('current_time =',current_time.format());
console.log( active_time.isAfter(current_time) );
<script src="https://rawgit.com/moment/moment/develop/moment.js"></script>

回答by Pierre Inglebert

If your dates are ISO8601 formatted or timestamp, don't use moment.isAfter. It's 150 times slower than comparing 2 dates objects : http://jsperf.com/momentjs-isafter-performance

如果您的日期是 ISO8601 格式或时间戳,请不要使用 moment.isAfter。它比比较 2 个日期对象慢 150 倍:http: //jsperf.com/momentjs-isafter-performance

 var active_time = new Date('2015-06-04T15:00Z');
 var current_time = new Date('2015-06-04T16:00Z');

 console.log('active_time =',active_time);
 console.log('current_time =',current_time);
 console.log( active_time > current_time );

回答by dave

Look at the toDatemethod to see what the internal js date is:

toDate方法看看内部js日期是什么:

console.log('active_time =',active_time.toDate());
console.log('current_time =',current_time.toDate());
console.log( active_time.isAfter(current_time) ); //Why does this return true?

active_time = Thu Jun 04 2015 15:00:00 GMT-0700 (Pacific Daylight Time)
current_time = Thu Jun 04 2015 09:00:00 GMT-0700 (Pacific Daylight Time)
true

It's going to depend on what timezone you are in

这将取决于您所在的时区