Moment.JS Date Filter Test

by Gabriel Correa

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.0/underscore-min.js"></script>

JavaScript

$.ajax({
  type: "GET",
  dataType: "JSON",
  url: 'https://famagas-api.herokuapp.com/api/itapeCompanyNews',
  success: function(allResponseThree) {
    // Native Array
    var receivedDataThree = [];

    // Group Received JSON By Total Of Purchased
    for (var i = 0; i < allResponseThree.length; i++) {
      // Group Received JSON By Total Of Purchased
      var totalOfPurchased = allResponseThree[i].purchase_history;
      for (var x = 0; x < totalOfPurchased.length; x++) {
        // Declare Variables Containing Extend Details
        var purchasedProductNested = totalOfPurchased[x].purchasedProduct;
        var dateOfPurchased = totalOfPurchased[x].dateOfPurchased;
        var quantityOfPurchasedNested = totalOfPurchased[x].quantityPurchased;
        var totalOfPurchasedNested = totalOfPurchased[x].totalOfPurchased;
        var totalOfDiscountsPurchasedNested = totalOfPurchased[x].discountsToConsider;

        // Format The Money So That It Is Possible To Count
        totalOfPurchasedNested = totalOfPurchasedNested.replace(/\./g, "").replace(",", ".");
        totalOfDiscountsPurchasedNested = totalOfDiscountsPurchasedNested.replace(/\./g, "").replace(",", ".");

        // Final JSON Array Containing Extend Details
        var allResponseThreeWithExtendDetails = {
          product: purchasedProductNested,
          dateOfPurchased: dateOfPurchased,
          quantity: JSON.parse(quantityOfPurchasedNested),
          totalPurchased: JSON.parse(totalOfPurchasedNested),
          totalDiscounts: JSON.parse(totalOfDiscountsPurchasedNested)
        };

        // Push Group With Received JSON In Native Array
        receivedDataThree.push(allResponseThreeWithExtendDetails);
      }
    }

    // Set Locale For Moment JS
    moment.locale("pt-BR");
    var todayDate = moment();

    var sortDate = _.filter(receivedDataThree, function(desc) {
      return moment(desc.dateOfPurchased, "DD/MM/YYYY").isAfter(todayDate.subtract(30, 'days'))
    });

   ...