我不明白为什么这个日期比较在js中不起作用。我已将json文件与具有dateproperties的项链接到nggrid。这就是刷新功能的样子:
$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);
}
});
});
};
网格的数据如下所示:
[
{
"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"
}
]
我希望看到我的表中显示5个日期,因为它们都满足请求?
答案 0 :(得分:1)
为什么不这样做呢:
var itemDate = new Date(item.date);
var compareDate = new Date('1/1/2025');
if (itemDate < compareDate) {
.....
}
答案 1 :(得分:0)
为什么不直接比较时间戳,如下所示: -
var itemDate = new Date(item.date).getTime();
var compareDate = new Date('1/1/2025').getTime();
if (itemDate < compareDate) {
}