Need the creation time of a MongoDB document from its _id? Paste an ObjectId above to get Unix seconds, UTC, and ISO 8601, or pick a date and mint a new ObjectId. This MongoDB ObjectId converter is part of the programming tools on TechOpt.io, next to the Unix timestamp converter.
What this MongoDB ObjectId converter reads from an id
A MongoDB ObjectId is 12 bytes, written as 24 hex characters, as documented in the ObjectId reference. Since MongoDB 3.4 the layout is:
| Bytes | Hex | Meaning |
|---|---|---|
| 0–3 | 8 characters | Unix time in seconds (big-endian) |
| 4–8 | 10 characters | Random value |
| 9–11 | 6 characters | Incrementing counter |
The timestamp is the only part this tool needs for a date. The random and counter bytes keep IDs unique when many documents are inserted in the same second. Older drivers used a machine id and process id instead of the 5 random bytes; the first 4 bytes were still seconds since the Unix epoch.
Seconds vs milliseconds
ObjectId timestamps are seconds, not milliseconds. A current value is about 10 digits. The millisecond figure this converter shows is that second multiplied by 1000, so it always ends in 000. Two documents created 200 ms apart in the same second share an ObjectId timestamp.
A BSON Date / ISODate is different: it is milliseconds since the epoch, the same as JavaScript’s Date. Convert those on the Unix timestamp converter. Do not paste an ISODate into the ObjectId field.
ObjectId.getTimestamp() in code
Drivers expose the same 4-byte timestamp this page reads. Examples:
mongosh
ObjectId("507f1f77bcf86cd799439011").getTimestamp()
ObjectId.createFromTime(1359849600)
const { ObjectId } = require('mongodb');
ObjectId.createFromHexString('507f1f77bcf86cd799439011').getTimestamp();
ObjectId.createFromTime(1359849600);
from bson import ObjectId
ObjectId('507f1f77bcf86cd799439011').generation_time
ObjectId.from_datetime(dt) # dt must be UTC
FAQ
How do I get a Unix timestamp from a MongoDB ObjectId?
Take the first 8 hex characters of the 24-character id and parse them as a big-endian unsigned 32-bit integer. That integer is seconds since 00:00:00 UTC on 1 January 1970. Paste the id above, or call ObjectId("…").getTimestamp() in mongosh.
Why is the ObjectId timestamp in seconds, not milliseconds?
The spec stores four bytes of Unix time, which only has room for seconds. Millisecond output on this page is seconds × 1000. BSON Date / ISODate values are milliseconds; convert those with the Unix timestamp converter, not this tool.
Can two documents share the same ObjectId timestamp?
Yes. Precision is one second. Inserts in the same second get the same timestamp bytes and differ in the random and counter fields. Do not treat the ObjectId timestamp as a unique clock reading.
How do I create an ObjectId from a date in code?
Use ObjectId.createFromTime(seconds) in the Node driver or mongosh, or ObjectId.from_datetime(dt) in PyMongo. Those fill the timestamp bytes; remaining bytes are zeros or random depending on the driver. This page always uses 8 random bytes after the timestamp.
Does the Year 2038 problem affect ObjectIds?
No. Signed 32-bit time_t overflows on 19 January 2038. ObjectId timestamps are unsigned 32-bit seconds, so they run through 7 February 2106. After that the 4-byte field wraps. This converter rejects dates outside 1970–2106.
Does this ObjectId converter upload my IDs?
No. It runs entirely in your browser. ObjectIds and dates never leave your machine.
How do I convert a BSON Date or ISODate?
A BSON Date is milliseconds since the Unix epoch, not an ObjectId. Use the Unix timestamp converter for ISODate strings and epoch milliseconds. This page only reads the 4-byte timestamp inside an ObjectId.
Looking for more developer how-tos? Browse the programming guides or the rest of the tools.