Filter by Last (Week, Month, Year) using Javascript

Filter by Last (Week, Month, Year) using Javascript

ยท

3 min read

Hello everyone, I hope everyone is safe during this pandemic. Today I am sharing a small article to implement the filter functionality on dates.

Introduction

This article focuses on the implementation of 3 commonly used filter methods(Filter by Week, Filter by Month, and Filter by Year) using javascript.

Dependency: Moment

Behind the story

A few days back, I appeared for a screening round for a startup company, which asked to have a functionality where they could filter the date property of an object with last week, last month, and last year. To my surprise, most of the helper functions that are out would return the dates within the last 7 days of last 30 days and the last 365 days for a filter by last week, last month, and last year respectively.

Explaination of the functionality

But my use case(mostly the right way of implementing) is different. For example, consider the date 14 July 2021(Wednesday), for that date, the prev week dates range should be 4 July 2021(start date of prev week i.e a Monday) to 10 July 2021(end day of prev week i.e a Saturday) not 7th July to 14 July(last 7 days). Similarly for the prev month and prev year as well.

Moment Library

Moment is a JavaScript date library for parsing, validating, manipulating, and formatting dates. With the help of the moment date library's helper function, the implementation becomes a piece of cake. Below I have shared the implementation for the functionalities of filters.

What am I using?

I am using an array of launches objects where each launch has a launch_date_utc date property. The below functions check if the launch happened the last week, last month, or last year depending on the filter chosen. Feel free to modify the codebase according to your use case.

Filter by Last Week

import moment from 'moment'

export function filterByLaunchDateLastWeek(launches: any) {
  const todayDate = new Date()
  const startDayOfPrevWeek = moment(todayDate).subtract(1, 'week').startOf('week').format('LLLL')
  const lastDayOfPrevWeek = moment(todayDate).subtract(1, 'week').endOf('week').format('LLLL')

  return launches.filter((launch:any) => {
    const launchDate = launch.launch_date_utc
    return moment(launchDate).isBetween(startDayOfPrevWeek, lastDayOfPrevWeek)
  })
}

Filter by Last Month

export function filterByLaunchDateLastMonth(launches: any) {
  const todayDate = new Date()
  const startDayOfPrevMonth = moment(todayDate).subtract(1, 'month').startOf('month').format('LLLL')
  const lastDayOfPrevMonth = moment(todayDate).subtract(1, 'month').endOf('month').format('LLLL')

  return launches.filter((launch:any) => {
    const launchDate = launch.launch_date_utc
    return moment(launchDate).isBetween(startDayOfPrevMonth, lastDayOfPrevMonth) 
  })
}

Filter by Last Year

export function filterByLaunchDateLastYear(launches: any) {
  const todayDate = new Date()
  const startDayOfPrevYear = moment(todayDate).subtract(1, 'month').startOf('month').format('LLLL')
  const lastDayOfPrevYear = moment(todayDate).subtract(1, 'month').endOf('month').format('LLLL')

  return launches.filter((launch:any) => {
    const launchDate = launch.launch_date_utc
    return moment(launchDate).isBetween(startDayOfPrevYear, lastDayOfPrevYear) 
  })
}

If you find this article helpful, feel free to like and support my work. Feedbacks are highly welcome. :)

Dont forget to connect with me on twitter @alisham__ ๐Ÿ˜‰

ย