如何根据javascript中的日期对数据进行排序

时间:2017-09-04 07:08:16

标签: javascript sorting comparison-operators

我有一些记录,包括过去2个月的订单如下:

[  
{"first_name":"Ambarish","last_name":"Bhattacharya","cell_phone":"7720046301","date":"17-07-06"},  
{"first_name":"Shweta","last_name":"Gaikwad","cell_phone":"9860853311","date":"17-07-06"},  
{"first_name":"Vijay","last_name":"Khot","cell_phone":"9923422773","date":"17-07-06"},  
{"first_name":"Gaurav ","last_name":"Sharma","cell_phone":"8939894922","date":"17-07-06"},  
{"first_name":"Kusum","last_name":"Barate","cell_phone":"9975580430","date":"17-07-06"},  
{"first_name":"Sachin","last_name":"Jagtap","cell_phone":"9767737770","date":"17-07-06"},  
{"first_name":"Sukla ","last_name":"Paul ","cell_phone":"8411813680","date":"17-07-06"},  
{"first_name":"Pallavi","last_name":"Raut","cell_phone":"9561093916","date":"17-07-06"},  
{"first_name":"Pranay","last_name":"Mahajan","cell_phone":"9767727614","date":"17-07-06"}  
]

现在我想在基于日期范围的四个类别中显示这些数据:  1. 0到15天  2. 15至30天  3. 30至45天  4. 45至60天

我试过这些条件语句:

if((_15DaysAgo_date<order_date || order_date<=today_date) && (_15DaysAgo_month<=order_month && order_month<=today_month)){
    $("#_15days").append(orders);
}else if((_30DaysAgo_date<order_date || order_date<=_15DaysAgo_date) && (_30DaysAgo_month<=order_month && order_month<=_15DaysAgo_month)){
    $("#_15to30Days").append(orders);
}else if((_45DaysAgo_date<order_date || order_date<=_30DaysAgo_date) && (_45DaysAgo_month<=order_month && order_month<=_30DaysAgo_month)){
    $("#_30to45Days").append(orders);
}else{
    $("#_45PlusDays").append(orders);
}

但它无法正常工作。任何人都可以帮我处理条件语句。提前致谢

1 个答案:

答案 0 :(得分:0)

您可以使用此代码。

function subtractDays(date, daysToSubtract){
    var tempDate = new Date(date);
    tempDate.setDate(date.getDate()-daysToSubtract);
    return tempDate;
}

var today = new Date();
if(order_date>subtractDays(today,15){
    ("#_15days").append(orders);
}else if(order_date>subtractDays(today,30){
    $("#_15to30Days").append(orders);
}

你也可以尝试使用具有很多功能的时刻来操纵日期和时间。