pajasv
pajasv

Reputation: 111

Get date between two dates using Lodash and Moment

I'm trying to get records with a date between two dates (provided by URL). A am using Lodash and Moment. I tried it in the following way but the result is empty. Does somebody have any idea? Thank you.

app.get('/paymentListByDate/:from/:to/', (req, res) => {
  let response = getDataPayment();
  let from_url = req.params.from;
  let to_url = req.params.to;
  let dateFormat = "DD-MM-YYYY";
  let filtered = _.filter(response, { 'dueDate': moment().isBetween(moment(from_url, dateFormat), moment(to_url, dateFormat))});
  sendDelayedResponse(res, filtered, 1);
});

sendDelayedResponse() method should be fine. Parameters as well. JSON object is the following:

  {
    "value": {"amount": 1000, "currency": "CZK"},
    "partyAccount": {"prefix": "035", "accountNumber": "123456789", "bankCode" :"2010"},
    "dueDate": "15.01.2020",
    "recurringPayment": {"firstPayment": "First payment", "lastPayment": "Last payment", "interval": "WEEK"},
    "payeeMessage": "Message2",
    "payerMessage": "Message1",
    "categoryId": 0,
    "additionalInfo": {"constantSymbol": "123456789", "variableSymbol": "123456789", "specificSymbol": "123456789"},
    "id": 1,
    "accountId": 123456789,
    "editableByUser": true,
    "realizationStatus": "RTS_REALISED"
  }

Upvotes: 0

Views: 3661

Answers (1)

hoangdv
hoangdv

Reputation: 16147

moment() returns current date time. You have to compare from_url with to_url (I don't know why you use _url instead of _date).

filter just working with a collection, I hope getDataPayment() returns a array in any cases.

Without lodash:

const filtered = response.filter((o) => {
  return moment(o.dueDate, 'DD.MM.YYYY') // Convert to moment with exactly date format
    .isBetween(moment(from_url, dateFormat), moment(to_url, dateFormat));
});

with lodash:

const filtered = _.filter(response, (o) => {
  return moment(o.dueDate, 'DD.MM.YYYY')
    .isBetween(moment(from_url, dateFormat), moment(to_url, dateFormat));
});

Upvotes: 2

Related Questions