Generate random dates and times in various formats
Number of dates to generate (1-100)
Random time generator creates random timestamps, dates, and time values. Generate random dates within ranges, random times of day, Unix timestamps, and formatted datetime strings for testing, data generation, and scheduling simulations.
Calculate random values within specified bounds. For dates: random milliseconds between start and end dates. For times: random hours (0-23), minutes (0-59), seconds (0-59). Convert to desired format (ISO 8601, Unix timestamp, locale-specific).
Date range: generate random ms between start.getTime() and end.getTime(). Time: random in 86400 seconds/day. Consider: timezones, DST transitions, leap seconds. Formats: ISO 8601 (2024-01-15T14:30:00Z), Unix (1705329000), human-readable.
Generate random timestamps for database seeding.
Random order dates for e-commerceCreate varied appointment times.
Generate realistic timestamp sequences.
Random event timing and cooldowns.
// Random date within range
function randomDate(start, end) {
const startMs = start.getTime();
const endMs = end.getTime();
const randomMs = startMs + Math.random() * (endMs - startMs);
return new Date(randomMs);
}
// Random time of day
function randomTimeOfDay() {
const hours = Math.floor(Math.random() * 24);
const minutes = Math.floor(Math.random() * 60);
const seconds = Math.floor(Math.random() * 60);
return { hours, minutes, seconds };
}
// Random timestamp with constraints
function randomTimestamp(options = {}) {
const {
start = new Date('2020-01-01'),
end = new Date(),
businessHoursOnly = false,
excludeWeekends = false
} = options;
let date;
do {
date = randomDate(start, end);
if (businessHoursOnly) {
const time = randomTimeOfDay();
time.hours = 9 + Math.floor(Math.random() * 8); // 9-17
date.setHours(time.hours, time.minutes, time.seconds);
}
} while (excludeWeekends && [0, 6].includes(date.getDay()));
return date;
}
// Generate multiple random times
function generateTimeSeries(count, interval = { min: 60000, max: 300000 }) {
const times = [];
let current = new Date();
for (let i = 0; i < count; i++) {
times.push(new Date(current));
const gap = interval.min + Math.random() * (interval.max - interval.min);
current = new Date(current.getTime() + gap);
}
return times;
}
// Format examples
const date = randomTimestamp();
console.log(date.toISOString()); // 2024-03-15T10:30:45.123Z
console.log(date.getTime()); // 1710498645123 (Unix ms)
console.log(Math.floor(date.getTime()/1000)); // 1710498645 (Unix s)Generates random timestamps with options for date ranges, business hours, and weekday constraints.
Use random time generator for test data, scheduling simulations, log generation, or any scenario requiring random datetime values.
ISO 8601 (YYYY-MM-DDTHH:MM:SS), Unix timestamp (seconds since 1970), locale strings (toLocaleString), custom formats. Most databases accept ISO 8601.