MongoDB ObjectId to Timestamp Converter
Decode a MongoDB ObjectId to its creation date, or turn a date into an ObjectId for _id range queries. Free and runs in your browser.
Runs in your browser. Nothing is uploaded.
Example
Input:
507f1f77bcf86cd799439011
Result:
2012-10-17T21:13:27.000Z (Unix seconds 1350508407)
How an ObjectId stores its date
A MongoDB ObjectId is 12 bytes, shown as 24 hexadecimal characters. The first 4 bytes are a big-endian Unix timestamp in seconds. The next 5 bytes are a random value generated once per process, and the last 3 bytes are an incrementing counter that starts at a random number.
Because the timestamp comes first, ObjectIds sort roughly by creation time, and you can read the creation date of any document straight from its _id without a separate createdAt field. This tool takes the first 8 hex characters, converts them to a number and shows the matching date in UTC and in your local time zone.
Date to ObjectId for range queries
To find every document created after a given moment, build an ObjectId from that date with the remaining 8 bytes set to zero, then query with $gte on _id. This uses the default _id index, so it is fast and needs no extra index on createdAt.
In the shell that looks like db.orders.find({ _id: { $gte: ObjectId("6540a0800000000000000000") } }). In Mongoose you can pass a string and it is cast to an ObjectId for you. The date mode here produces that boundary ObjectId so you do not have to compute the hex by hand.
Limits to keep in mind
The timestamp has one-second resolution, and it records when the ObjectId was generated, which is normally the client or driver, not the database server. Documents inserted with a custom _id, or imported from another system, may carry no useful date at all. If the creation time matters to your business logic, store an explicit createdAt field and use the ObjectId date only as a quick forensic tool.
In Node.js you can do the same conversion in code: new Types.ObjectId(id).getTimestamp() in Mongoose returns a Date, and Types.ObjectId.createFromTime(seconds) builds the boundary id for a range query. This page is handy when you are reading logs, support tickets or a database dump and just need the date of one id without opening a shell.
Frequently asked questions
How is the date stored in a MongoDB ObjectId?
The first 4 bytes of the 12-byte ObjectId are a Unix timestamp in seconds, big-endian. That is the first 8 hex characters of the 24-character string.
Can I query documents by creation date using only _id?
Yes. Generate an ObjectId from the start date (the other 8 bytes zero) and query with $gte or $lt on _id. It uses the built-in _id index.
Is the ObjectId date always the insert time?
Not always. It is when the id was generated, usually on the driver side, and it has one-second resolution. Custom or imported _id values may have no meaningful date.
Is my data sent anywhere?
No. The conversion runs entirely in your browser.
