Last active
August 29, 2015 14:09
-
-
Save rhenning/899c77ba618752fd43c6 to your computer and use it in GitHub Desktop.
Mongo Date ObjectId helpers
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
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