简体   繁体   English

如何使用javaScript过滤日期?

[英]How to filter date using javaScript?

I'm creating one HTML report which has to show order details as per date selection. 我正在创建一个HTML报告,该报告必须显示按日期选择的订单详细信息。 The order details stored in JSON file as given below : 存储在JSON文件中的订单详细信息如下所示:

[
  {
    "storeName": "ireland",
    "langCode": "en_IE",
    "testCaseName": "Guest User Checkout - Credit Card",
    "orderID": "4001820209",
    "date": "12-02-2019 15:37:30.201",
    "cartTotal": "19.98"
  },
  {
    "storeName": "denmark",
    "langCode": "en_DK",
    "testCaseName": "Guest User Checkout - Credit Card",
    "orderID": "6001825084",
    "date": "12-02-2019 15:37:35.140",
    "cartTotal": "338 DKK"
  },
  {
    "storeName": "united_kingdom",
    "langCode": "en_GB",
    "testCaseName": "Guest User Checkout - Credit Card",
    "orderID": "7002541022",
    "date": "13-02-2019 15:45:08.038",
    "cartTotal": "13.04"
  }
]

The HTML report would be look like this : HTML报告将如下所示:

在此处输入图片说明

I want to take all order dates from this JSON file uniquely. 我想唯一地获取此JSON文件中的所有订单日期。 Time needs to be removed and if same date already added into the HTML then skip it. 需要删除时间,如果HTML中已经添加了相同的日期,请跳过该时间。 Expected date order on HTML should be : HTML的预期日期顺序应为:

  • 12-02-2019 12-02-2019
  • 13-02-2019 13-02-2019
  • 14-02-2019 14-02-2019

If user clicks on that particular date then it should display all order placed on that date. 如果用户单击该特定日期,则它应显示该日期下的所有订单。

I have written below jQuery/javascript function to read the date and add into html but not sure how to make them unique. 我在下面的jQuery / javascript函数中编写了读取日期并将其添加到html中的方法,但不确定如何使它们唯一。

This will call on page load 这将调用页面加载

function myFunction() {
    createList();
    $.getJSON('order-detail.json', function (item) {
        item.forEach(function (data) {
                addItemsInList(data);
        });
    });
}

Create un-ordered list 创建无序列表

function createList() {

    var ul = document.createElement('ul');
    ul.setAttribute('id', 'order-dates');

    var div = document.getElementById('orderDateContainer');
    div.appendChild(ul);
}

Append dates in the list 在列表中附加日期

function addItemsInList(data){

    var ul = document.getElementById('order-dates');
    li = document.createElement('li');
    li.setAttribute('class', 'ord-date');
    ul.appendChild(li);
    var a = document.createElement('a');
    a.setAttribute('class', 'record-viewer');
    a.setAttribute('href','#recordContent');
    a.innerHTML=data.date;
    a.setAttribute('onclick','tableCreater()');
    li.appendChild(a);
}

Any help would be great appreciated. 任何帮助将不胜感激。

With only slight modifications to your existing functions, you could only loop over the unique dates and add them as before. 仅需对现有功能进行少量修改,就只能循环唯一的日期,并像以前一样添加它们。

  1. In myFunction we now reduce the data to only unique strings of dates, stripping away the time. myFunction我们现在将数据缩减为仅唯一的日期字符串,从而节省了时间。 Logic of the reducer is extracted to collectIfUniqueDateFromOrder function. 减速器的逻辑被提取到collectIfUniqueDateFromOrder函数。

     function myFunction() { createList(); $.getJSON('order-detail.json', function (data) { const uniqueDateStrings = data.reduce(collectIfUniqueDateFromOrder, []); uniqueDateStrings.forEach(addItemsInList); }); } function collectIfUniqueDateFromOrder (uniqueDates, order) { var dateStringWithoutTime = order.date.split(" ")[0]; if (!uniqueDates.includes(dateStringWithoutTime)) { uniqueDates.push(dateStringWithoutTime); } return uniqueDates; } 
  2. In addItemsInList we only handle date strings addItemsInList我们仅处理日期字符串

     function addItemsInList(dateString) { var ul = document.getElementById('order-dates'); var li = document.createElement('li'); li.setAttribute('class', 'ord-date'); ul.appendChild(li); var a = document.createElement('a'); a.setAttribute('class', 'record-viewer'); a.setAttribute('href', '#recordContent'); a.innerHTML = dateString; // <-- a.setAttribute('onclick', 'tableCreater()'); li.appendChild(a); } 

You can use moment js to parse dates so you can change date format and process things with date object and get a unique list by Set 您可以使用moment js解析日期,以便更改日期格式并使用date object处理事物,并通过Set获得唯一列表

OR you can just use substring to get your date by dateTimeString.substr(0,10) 或者,您也可以使用子字符串通过dateTimeString.substr(0,10)获取日期

 const datetime_dates = [ "12-02-2019 15:37:30.201", "12-02-2019 15:37:30.201", "13-02-2019 15:37:30.201", "13-02-2019 15:37:30.201", "13-02-2019 15:37:30.201", "14-02-2019 15:37:30.201", ]; const get_unique_dates = (dates) => { const dates_only = dates.map(date => moment(date, "DD-MM-YYY HH:mm:ss.SSS").format("DD-MM-YYYY")); const unique_dates = [...new Set(dates_only)]; return unique_dates; } console.log(get_unique_dates(datetime_dates)); // example with your data const orders = [ { "storeName": "ireland", "langCode": "en_IE", "testCaseName": "Guest User Checkout - Credit Card", "orderID": "4001820209", "date": "12-02-2019 15:37:30.201", "cartTotal": "19.98" }, { "storeName": "denmark", "langCode": "en_DK", "testCaseName": "Guest User Checkout - Credit Card", "orderID": "6001825084", "date": "12-02-2019 15:37:35.140", "cartTotal": "338 DKK" }, { "storeName": "united_kingdom", "langCode": "en_GB", "testCaseName": "Guest User Checkout - Credit Card", "orderID": "7002541022", "date": "13-02-2019 15:45:08.038", "cartTotal": "13.04" } ] const order_dates = orders.map(order => order.date); const unique_order_dates = get_unique_dates(order_dates); console.log(unique_order_dates); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script> 

For filtering, you better do it in the backend for performance wise by requesting ajax every time user click to your date link 为了进行过滤,最好在后端执行此操作以提高性能,方法是每次用户单击date link都请求ajax

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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