0
votes

I fail to see why this date comparison is not working in js. I have linked a json file with items that have dateproperties to the nggrid. This is what the refresh function looks like:

$scope.refresh = function() {
        $http.get('data.jon').success(function(data) {
          //$scope.myData = data;
          //$scope.myData.splice(0,1);
          var i=0;
          data.forEach(
            function(item) {
              i++;
              console.log(i);
              var itemDate = new Date(item.date).valueOf();
              var compareDate = new Date('1/1/2025').valueOf();
              if (itemDate < compareDate) {
                debugger;
                console.log(itemDate);
                console.log(compareDate);
                $scope.myData.push(item);
              }
            });
        });
};

The data for the grid looks like this:

[
    {
        "name": "Moroni",
        "age": 50,
        "date":"1/1/2015"
    },
    {
        "name": "Tiancum",
        "age": 43,
        "date":"1/1/2016"
    },
    {
        "name": "Jacob",
        "age": 27,
        "date":"1/1/2017"
    },
    {
        "name": "Nephi",
        "age": 29,
        "date":"1/1/2018"
    },
    {
        "name": "Enos",
        "age": 34,
        "date":"1/1/2019"

    }
]

I would expect to see 5 dates to be displayed in my table because they all satisfy the request?

Here is a plunker:http://plnkr.co/edit/e4ua6k?p=preview

2

2 Answers

1
votes

Why don't you do this instead:

var itemDate = new Date(item.date);
var compareDate = new Date('1/1/2025');

if (itemDate < compareDate) {
  .....
}
0
votes

Why not compare timestamp directly instead like below :-

var itemDate = new Date(item.date).getTime();
var compareDate = new Date('1/1/2025').getTime();

if (itemDate < compareDate) {

}