日期比较无法在Angular JS中工作 [英] Dates comparing not get work in angular js

查看:77
本文介绍了日期比较无法在Angular JS中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我为什么我的约会不成功. 基本上,当我尝试比较日期时,它在angularJs中不起作用

Can anyone tell me why my date is not get working. Basically when i try to compare date then is is not working in angularJs

  var dateObj1 = $filter("date")(Date.now(), 'dd-MMM-yyyy'); // output is "04-May-2016"
    var dateObj2 = $scope.employee.Tue; // output is "03-May-2016"
    if (dateObj1 < dateObj2) {         
        return true
    } else {
        return false;
    }

以上工作正常,但如果我使用date作为"2016年4月26日",则在以下情况下不工作

above is working but for case below is not working if i use date as "26-Apr-2016" i get true in return

 var dateObj1 = $filter("date")(Date.now(), 'dd-MMM-yyyy'); // output is "04-May-2016"
    var dateObj2 = $scope.employee.Tue; // output is "26-Apr-2016"
    if (dateObj1 < dateObj2) {         
        return true
    } else {
        return false;
    }

推荐答案

根据日期过滤器,此过滤器

根据请求的格式将日期设置为字符串格式.

Formats date to a string based on the requested format.

因此,在将dateObj1与dateObj2比较时,您使用的是按字典顺序排列的字符串比较.

So when comparing dateObj1 to dateObj2, you are using the string comparison which is the lexicographic order.

您必须将字符串解析为日期(使用Date.parse)以获得所需的结果

You must parse your string to a Date (by using Date.parse) to obtains the wanted results

这篇关于日期比较无法在Angular JS中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆