Published on

Day.js Date Filtering: isBefore and isAfter

Authors

Day.js, a lightweight JavaScript library using which you can work with date pretty easily.

Dependencies

npm i dayjs

isBefore

import dayjs from 'dayjs'

const exampleTransactions = [
  {
    date: '2023-04-01T00:00:00.000+00:00',
    description: 'UPI-GOOGLEPAY-goog-payment@okaxis',
    amount: 201,
  },
  {
    date: '2023-04-05T00:00:00.000+00:00',
    description: 'UPI-Mr XXXXXX X-xxxxxxxxx@okaxis',
    amount: 251,
  },
];

const filteredTransactions = exampleTransactions.filter(
  (transaction) => dayjs(transaction.date).isBefore('2023-05-05')
);

isAfter

import dayjs from 'dayjs';

const exampleTransactions = [
  {
    date: '2023-04-01T00:00:00.000+00:00',
    description: 'UPI-GOOGLEPAY-goog-payment@okaxis',
    amount: 201,
  },
  {
    date: '2023-04-05T00:00:00.000+00:00',
    description: 'UPI-Mr XXXXXX X-xxxxxxxxx@okaxis',
    amount: 251,
  },
];

const filteredTransactions = exampleTransactions.filter(
  (transaction) => dayjs(transaction.date).isAfter('2023-03-01')
);

Happy parsing date!