Alternatives to a Dedicated 'previousDay' Function in date-fns


  1. Using subDays

The subDays function from date-fns subtracts a specified number of days from a given date object. You can use it like this:

const dateFns = require('date-fns');
const previousDay = dateFns.subDays(new Date(), 1);

This code snippet creates a new Date object representing the current date and then uses subDays to subtract one day, effectively giving you the previous day.

  1. Using startOfDay and manual subtraction

Another approach is to use startOfDay to get the start of the current day at midnight (00:00 hours) and then subtract one day using regular JavaScript arithmetic.

const dateFns = require('date-fns');
const today = dateFns.startOfDay(new Date());
const previousDay = new Date(today.getTime() - (1000 * 60 * 60 * 24));


Example 1: Using subDays

const dateFns = require('date-fns');

// Get today's date
const today = new Date();

// Get the previous day
const previousDay = dateFns.subDays(today, 1);

console.log("Today:", today.toDateString()); // Output: Today: Fri 2024-06-14 (assuming today is Friday)
console.log("Previous Day:", previousDay.toDateString()); // Output: Previous Day: Thu 2024-06-13

This example imports date-fns, creates a today object with the current date, and uses subDays to subtract one day, storing the result in previousDay. Finally, it logs both dates for comparison.

const dateFns = require('date-fns');

// Get the start of today (midnight)
const today = dateFns.startOfDay(new Date());

// Calculate milliseconds in a day
const millisecondsPerDay = 1000 * 60 * 60 * 24;

// Get the previous day by subtracting milliseconds
const previousDay = new Date(today.getTime() - millisecondsPerDay);

console.log("Today:", today.toDateString()); // Output: Today: Fri 2024-06-14 (assuming today is Friday)
console.log("Previous Day:", previousDay.toDateString()); // Output: Previous Day: Thu 2024-06-13


  1. Conditional Logic

If you only need to check if a date is yesterday, you can use conditional logic with getDate and potentially getTime from the native JavaScript Date object.

const today = new Date();
const yesterday = new Date(today.getTime() - (1000 * 60 * 60 * 24));

if (yesterday.getDate() === today.getDate() - 1) {
  console.log("This date is yesterday");
}

This approach compares the difference in days between the current date and the candidate date.

  1. isYesterday Function

You can create your own custom function to check if a date is yesterday:

function isYesterday(date) {
  const today = new Date();
  return date.getDate() === today.getDate() - 1;
}

const yesterday = new Date(2024, 5, 14); // June 14th, 2024

if (isYesterday(yesterday)) {
  console.log("This date is yesterday");
}

This function takes a date as input and compares it with yesterday's date.

  1. Libraries with Built-in Functionality

While date-fns doesn't have a specific previousDay function, some other date manipulation libraries might offer such functionality. Explore alternatives like Moment.js or Luxon to see if they provide dedicated methods for getting the previous day.