Skip to content

Instantly share code, notes, and snippets.

@jayantbh
Last active September 21, 2017 20:02
Show Gist options
  • Save jayantbh/5dd72ee43b1f81c0f6f7a2a1509a6140 to your computer and use it in GitHub Desktop.
Save jayantbh/5dd72ee43b1f81c0f6f7a2a1509a6140 to your computer and use it in GitHub Desktop.
Generate a random date within a given range | https://jsfiddle.net/jayantbh/6x2gcnxn/2/
// Basic Random Date Generator
function randomInt(a = 0, b = 0) { return (a + window.crypto.getRandomValues(new Uint8Array(1))[0] % Math.abs(b - a + 1)); }
function log(json) {
for (key in arguments) {
let string = JSON.stringify(arguments[key], null, ' ');
document.body.innerHTML += `<pre>${string}</pre>&nbsp;`;
}
console.log(...arguments);
document.body.innerHTML += '<br>';
}
function getDaysInMonth (month, year) {
const isLeapYear = year % 4 === 0;
switch(month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; break;
case 4: case 6: case 9: case 11: return 30; break;
case 2: return isLeapYear ? 29 : 28;
}
}
function getRandomDayInRange (start, end) {
if (typeof start !== 'object' || typeof end !== 'object') { throw new Error('Incorrect parameter type. `start` and `end` need to be objects with properties `day`, `month` and `year`.'); }
if (!start.day) { throw new Error('`start.day` needs to be defined.')}
if (!start.month) { throw new Error('`start.month` needs to be defined.')}
if (!start.year) { throw new Error('`start.year` needs to be defined.')}
if (!end.day) { throw new Error('`end.day` needs to be defined.')}
if (!end.month) { throw new Error('`end.month` needs to be defined.')}
if (!end.year) { throw new Error('`end.year` needs to be defined.')}
const year = randomInt(start.year, end.year);
const isStartYear = year === start.year;
const isEndYear = year === end.year;
const isStartYearButNotSameAsEndYear = isStartYear && !isEndYear;
const isEndYearButNotSameAsStartYear = isEndYear && !isStartYear;
const isNeitherStartNorEndYear = !isStartYear && !isEndYear;
if (isStartYearButNotSameAsEndYear) { end.month = 12; }
if (isEndYearButNotSameAsStartYear) { start.month = 1; }
if (isNeitherStartNorEndYear) { start.month = 1; end.month = 12; }
const month = randomInt(start.month, end.month);
const isStartMonth = month === start.month;
const isEndMonth = month === end.month;
const isFirstMonthButNotLast = isStartYear && isStartMonth && !isEndYear && !isEndMonth;
const isLastMonthButNotFirst = isEndYear && isEndMonth && !isStartYear && !isStartMonth;
const isNotFirstMonth = isStartYear && !isStartMonth || !isStartYear;
const isNotLastMonth = isEndYear && !isEndMonth || !isEndYear;
if (isFirstMonthButNotLast) { end.day = getDaysInMonth(month, year); }
if (isLastMonthButNotFirst) { start.day = 1; }
if (isNotFirstMonth) { start.day = 1; }
if (isNotLastMonth) { end.day = getDaysInMonth(month, year); }
if (isNeitherStartNorEndYear) { start.day = 1; end.day = getDaysInMonth(month, year); }
const day = randomInt(start.day, end.day);
return [day, month, year];
}
function parseDate (dates) {
const DATE_REGEX = /\d{1,2}\-\d{1,2}\-\d{4}/;
if (typeof startDate !== 'string' || typeof endDate !== 'string') { throw new Error('Incorrect input type'); }
if (!(DATE_REGEX.test(startDate) && DATE_REGEX.test(endDate))) { throw new Error ('Incorrect date format'); }
let [startDay, startMonth, startYear] = startDate.split('-').map(_ => parseInt(_));
let [endDay, endMonth, endYear] = endDate.split('-').map(_ => parseInt(_));
return [
{ day: startDay, month: startMonth, year: startYear },
{ day: endDay, month: endMonth, year: endYear }
];
}
// ===========================================================================
let [ startDate, endDate ] = ['10-06-2016', '20-06-2016'];
let [ start, end ] = parseDate(startDate, endDate);
const LIMIT = 20;
for (let i = 0; i < LIMIT; i++) {
let [day, month, year] = getRandomDayInRange(start, end);
log(day, month, year);
}
log('Thanks for trying this out!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment