如何根据 javascript 中的日期对数据进行排序
how to sort the data on the basis of dates in javascript
我有一些记录,其中包括过去 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);
}
但是它不能正常工作。谁能帮我做条件语句。提前致谢
您可以使用此代码。
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);
}
你也可以尝试使用moment,它有很多操作日期和时间的函数。
我有一些记录,其中包括过去 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);
}
但是它不能正常工作。谁能帮我做条件语句。提前致谢
您可以使用此代码。
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);
}
你也可以尝试使用moment,它有很多操作日期和时间的函数。