// Generate a random date between two years using Date
function randomDate(startYear, endYear) {
// Convert start and end years to timestamps
const start = new Date(startYear, 0, 1); // January = 0
const end = new Date(endYear, 11, 31); // December = 11
const startTs = start.getTime();
const endTs = end.getTime();
// Uniform distribution over the timestamp range
const randomTs = Math.floor(Math.random() * (endTs - startTs + 1)) + startTs;
// Convert back to Date
return new Date(randomTs);
}
const dates = [];
for (let i = 0; i < 10; i++) {
dates.push(randomDate(1990, 2030));
}
for (const d of dates) {
console.log(`${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}`);
}
/*
run:
1995-1-9
1993-1-6
1997-10-19
2030-7-2
2004-10-20
2022-2-27
1994-6-22
2008-7-13
2030-5-19
1990-7-3
*/