Yevhen Klymentiev
dark
light
console
darkness
y.klymentiev@gmail.com
Reusable Snippets|Practical utility code for everyday use — custom-built and ready to share

isPast

Checks if the given date is in the past.

TypeScript
Copied!
1/**
2 * Checks if the given date is in the past.
3 *
4 * @param date - The date to check.
5 * @returns True if the date is before the current time, false otherwise.
6 */
7export function isPast(date: Date): boolean {
8  return date.getTime() < Date.now();
9}
  • Simple and Fast

    Uses a direct millisecond comparison for optimal performance and minimal overhead.

  • No External Dependencies

    Pure vanilla implementation without relying on external date libraries.

  • Clear Semantic Purpose

    The function name and logic are tightly aligned, making it highly readable and intuitive.

Tests | Examples

TypeScript
Copied!
1test('isPast - returns true for a date in the past', () => {
2  const past = new Date(Date.now() - 10000); // 10 seconds ago
3  expect(isPast(past)).toBe(true);
4});
5
6test('isPast - returns false for a date in the future', () => {
7  const future = new Date(Date.now() + 10000); // 10 seconds from now
8  expect(isPast(future)).toBe(false);
9});
10
11test('isPast - returns false for the current moment', () => {
12  const now = new Date();
13  // Give a small margin to avoid timing issues
14  expect(isPast(now)).toBe(false);
15});

Common Use Cases

  • Form Validation

    Prevent users from selecting a past date in scheduling or booking forms.

  • Expiration Checks

    Determine if a deadline, token, or event has already passed.

  • Conditional UI Rendering

    Show or hide elements based on whether a date has already occurred (e.g., “Event Over”).

  • Task and Reminder Systems

    Detect overdue items in to-do lists, calendars, or notifications.

Codebase: Utilities -> Dates -> isPast | Yevhen Klymentiev