Skip to content

Instantly share code, notes, and snippets.

@kennwhite
Created December 9, 2022 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kennwhite/521dca3ec741bc38e2d2d75ce857c093 to your computer and use it in GitHub Desktop.
Save kennwhite/521dca3ec741bc38e2d2d75ce857c093 to your computer and use it in GitHub Desktop.
Javascript/Node function to generate a random date set to UTC 00:00:00 timestamp
// Create a native Javascript Date object set to UTC 00:00:00 timestamp
// Here, the default parameters create a plausible DOB
function randomDate( start = new Date(1935, 0, 1), end = new Date(2004, 0, 1) ) {
var dt = new Date(+start + Math.random() * (end - start));
dt.setUTCHours(0,0,0,0);
return dt;
}
// Random DOB
db.people.insert({ "username" : "Pat", "dob" : randomDate() });
// Restricted dates, e.g., credit card dates: Jan 1 2020 - Dec 31 2028 (months are zero-offset, days are not)
db.people.insert({ "username": "baz", "dob": randomDate(new Date(2020, 0, 1), new Date(2028, 11, 31)) })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment