daysSince

A utility function to calculate the number of days since a given date.
> daysSince(date: Date): number
Parameters
  • date: The date to calculate the number of days from.
  • Returns: The number of days that have passed since the given date. If the date is today or in the future, it returns `0`. If the date is invalid, it also returns `0`.

Example

import { daysSince } from "@explita/daily-toolset";

// Calculate the number of days since a past date
const pastDate = new Date("2023-01-01");
console.log(daysSince(pastDate)); // Number of days since January 1st, 2023

// Check with today's date
const today = new Date();
console.log(daysSince(today)); // 0

// Check with a future date
const futureDate = new Date("2025-01-01");
console.log(daysSince(futureDate)); // 0

// Check with an invalid date
const invalidDate = new Date("invalid date string");
console.log(daysSince(invalidDate)); // 0

Use Cases

  • Tracking how many days have passed since a specific event.
  • Determining the age of an item or document based on the date it was created.
  • Validating if a time-sensitive process has exceeded a certain threshold.