Generates a random date within a specified range from a starting point.

Creates a currying function that accepts a range in milliseconds and returns a random date between the start date and start date + range. The range represents the maximum number of milliseconds to add to the starting date. Useful for generating timestamps, creation dates, or scheduling test data.

  const now = new Date();
const oneDay = 24 * 60 * 60 * 1000;
const oneMonth = 30 * oneDay;

// Random date within the next 30 days
const futureDate = RandomGenerator.date(now)(oneMonth);

// Random date within the past week
const pastWeek = new Date(now.getTime() - 7 * oneDay);
const recentDate = RandomGenerator.date(pastWeek)(7 * oneDay);

// Generate random creation dates for test data
const startOfYear = new Date(2024, 0, 1);
const endOfYear = new Date(2024, 11, 31).getTime() - startOfYear.getTime();
const randomCreationDate = RandomGenerator.date(startOfYear)(endOfYear);

// Create test events with random timestamps
const events = Array.from({ length: 50 }, () => ({
id: RandomGenerator.alphaNumeric(8),
title: RandomGenerator.name(2),
createdAt: RandomGenerator.date(new Date())(oneMonth),
scheduledFor: RandomGenerator.date(new Date())(oneMonth * 3)
}));
  • Parameters

    • from: Date

      The starting date for the random range

    Returns (range: number) => Date

    A currying function that accepts a range in milliseconds