简体   繁体   中英

how to sort the data on the basis of dates in javascript

i have some records which include orders from past 2 months as follows:

[  
{"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"}  
]

Now I want to display this data in fours categories which are based on date ranges as: 1. 0 to 15 days 2. 15 to 30 days 3. 30 to 45 days 4. 45 to 60 days

I have tried these conditional statements:

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);
}

But it is not working properly. Can anyone help me with the conditional statements. Thanks in advance

You can use this code.

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);
}

You can also try using moment which has a lot of functions for manipulating dates and times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM