Javascript/Node function to generate a random date set to UTC 00:00:00 timestamp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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