MongoDB ObjectId ↔ Timestamp Converter

Did you know that each MongoDB ObjectId contains an embedded timestamp of its creation time?
From the mongo shell, you can use getTimestamp() to retrieve the timestamp from the ObjectId, but there's no built in function to generate an ObjectId from a timestamp.
This online converter will convert the timestamp to an ObjectId and back.

ObjectId

(NOTE: not unique, only use for comparisons, not for creating new documents!)

ObjectId for pasting into mongo shell

Time (UTC)

Year (4 digits)
Month (1 - 12)
Day (1 - 31)
Hour (0 - 23)
Minute (0 - 59)
Second (0 - 59)
ISO Timestamp

Why generate an ObjectId from a timestamp?

To find all comments created after 2013-11-01:

db.comments.find({_id: {$gt: ObjectId("5272e0f00000000000000000")}})

Javascript functions

var objectIdFromDate = function (date) {
    return Math.floor(date.getTime() / 1000).toString(16) + "0000000000000000";
};
            
var dateFromObjectId = function (objectId) {
    return new Date(parseInt(objectId.substring(0, 8), 16) * 1000);
};