javascript - Javascript 对日期对象数组进行排序
问题描述
我有这个函数可以返回一个日期对象数组。
function get_location_dates(filter){
return filter.map(function(){
var [day, month, year] = $(this).text().split('-');
return new Date(parseInt(year), parseInt(month)-1, parseInt(day));
}).get();
}
$('#dropoff-locations .date:first').text();
"25-09-2019"
var dropoff = $('#dropoff-locations .date');
undefined
get_location_dates(dropoff);
(2) [Wed Sep 25 2019 00:00:00 GMT+0300 (East Africa Time), Tue Sep 24 2019 00:00:00 GMT+0300 (East Africa Time)]
我现在需要将它们从最早的日期排序到最旧/最新的日期。建议的问题并不完全适用于这些场景,因为与包含日期的数组的列表不同,这纯粹是一个日期数组。
我怎么做?
解决方案
这是我找到的解决方案
function get_location_dates(filter){
return filter.map(function(){
var [day, month, year] = $(this).text().split('-');
return new Date(parseInt(year), parseInt(month)-1, parseInt(day));
}).get().sort(function(a, b){
return a - b;
});;
}
推荐阅读
- c# - ASP.NET Core 5 Web API 上传文件
- odoo - 如何在 set_values 方法 odoo 14 中保存 one2many 字段
- scala - 谁能帮我解决这个错误,无法运行程序“npm”
- flutter - Flutter Tabcontroller 不要滑动标签直到填充当前
- sql-server - SSIS 中 OLE DB 参数的优先顺序是什么?
- c++ - shared_ptr 的实际用途?
- deep-learning - A CNN 中多维卷积数组上最大池化操作的最佳时间复杂度
- oracle - Oracle Wallet 创建 - 选项 -wrl 代表什么?
- c++ - C ++如何引用用户输入?
- html - “角色=横幅”在CSS和HTML中的含义是什么?