Mdd*_*Mdd 5 javascript momentjs
我有一个以UTC时间存储的字符串.我想看看这个时间是否在当前的UTC时间之后.我使用的是momentjs,当只有1小时的差异时,isAfter()方法返回不正确的值.
active_time变量发生在15:00 utc.current_time设置为16:00 utc.所以我认为active_time.isAfter(current_time)应该回来,false但它正在回归true.我怎样才能让它回归false?
jsFiddle链接:http://jsfiddle.net/Ln1bz1nx/
码:
//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?
Run Code Online (Sandbox Code Playgroud)
即使第一个日期字符串是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) );Run Code Online (Sandbox Code Playgroud)
<script src="https://rawgit.com/moment/moment/develop/moment.js"></script>Run Code Online (Sandbox Code Playgroud)
如果您的日期是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 );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12819 次 |
| 最近记录: |