Skip to content

Instantly share code, notes, and snippets.

@jlrjr
Created April 1, 2013 14:25
Show Gist options
  • Save jlrjr/5285225 to your computer and use it in GitHub Desktop.
Save jlrjr/5285225 to your computer and use it in GitHub Desktop.
Generate Random GlideDateTime for ServiceNow
/**
* Generate a random GlideDateTime
* If start and end are not specified you'll get a random date within the past year
*
* @param [start] GlideDateTime - optionally defines the start of the window
* @param [end] GlideDateTime - optionally defines the end of the window
* @return GlideDateTime
*/
function generateRandomDate(/*[GlideDateTime]*/start, /*[GlideDateTime]*/ end){
var temp = new GlideDateTime();
if (start instanceof GlideDateTime && end instanceof GlideDateTime) {
temp.setValue(start.getValue());
}
else {
temp.addYears(-1);
end = new GlideDateTime(); //now
}
var totalDays = Math.floor(gs.dateDiff(temp, end, true) / (60 * 60 * 24));
var rndSecs = Math.floor(Math.random() * (60 * 60 * 24)); //seconds in a day
temp.addSeconds(rndSecs);
var rndDays = Math.floor(Math.random() * totalDays) + 1;
temp.addDays(rndDays);
return temp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment