Skip to content

Instantly share code, notes, and snippets.

@rhenning
Last active August 29, 2015 14:09
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 rhenning/899c77ba618752fd43c6 to your computer and use it in GitHub Desktop.
Save rhenning/899c77ba618752fd43c6 to your computer and use it in GitHub Desktop.
Mongo Date ObjectId helpers
var daysAgo = 10;
Date.prototype.toObjectId = function() {
var dateSecs = Math.floor(this.getTime() / 1000);
return ObjectId(dateSecs.toString(16) + "0000000000000000");
}
function daysAgoToDate(daysAgo) {
return new Date(new Date().setDate(new Date().getDate() - daysAgo));
}
var objectIdAgo = daysAgoToDate(daysAgo).toObjectId();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment