Skip to content

Instantly share code, notes, and snippets.

@dieseltravis
Created July 25, 2008 17:41
Show Gist options
  • Save dieseltravis/2482 to your computer and use it in GitHub Desktop.
Save dieseltravis/2482 to your computer and use it in GitHub Desktop.
JavaScript version of this ruby: http://project.ioni.st/post/2288#snippet_2288
Date.prototype.atSomePoint = function(hours) {
var startHour = new Date(this.valueOf());
var endHour = new Date(this.valueOf());
startHour.setSeconds(0);
startHour.setMinutes(0);
endHour.setSeconds(0);
endHour.setMinutes(0);
if (hours) {
startHour.setHours(hours.between);
endHour.setHours(hours.and);
}
else {
startHour.setHours(0);
endHour.setHours(24);
}
return new Date(rand(startHour.valueOf(), endHour.valueOf()));
};
Number.prototype.am = function() {
return this;
};
Number.prototype.pm = function() {
return this + 12;
};
function rand(lowerBound, upperBound) {
return Math.floor((upperBound - (lowerBound - 1)) * Math.random()) + lowerBound;
}
new Date().atSomePoint(); // Fri Jul 25 2008 15:22:15 GMT-0400 (Eastern Daylight Time)
new Date().atSomePoint({between: (1).am(), and: (7).am()}); // Fri Jul 25 2008 04:24:29 GMT-0400 (Eastern Daylight Time)
new Date().atSomePoint({between: (1).pm(), and: (7).pm()}); // Fri Jul 25 2008 17:14:38 GMT-0400 (Eastern Daylight Time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment