Documentation
    Preparing search index...
    • Generates a random date within a specified range from a starting point.

      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.

      Processing cost: One integer draw and one Date construction use constant work.

      Parameters

      • from: Date

        The starting date for the random range

      • range: number

        The range in milliseconds from the starting date

      Returns Date

      A random date within the specified range

      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),
      }));

      contracts/common.md#principled-implementation For a valid start date and finite nonnegative integer range whose endpoint is Date-representable, the result adds an integer offset from zero through range. Unsupported invalid dates or bounds are not validated and can produce an Invalid Date or an out-of-range result.

      contracts/common.md#clear-and-simple-design One expression over the shared integer helper.

      contracts/common.md#prohibited-implementation-shortcuts The offset is drawn, not fixed.

      contracts/common.md#meaningful-documentation The comment states the interval and the unit of the range, with examples.