MongoDB ObjectId to Date
Every ObjectId carries its own creation time in its first eight characters; the other sixteen carry none. Paste an id and read the date out, or press NOW to see how a fresh id is built from the current second.
ObjectId → creation date
Enter a value to see results below. An ObjectId is 24 hex digits (12 bytes): the first 8 are big-endian unix seconds, the remaining 16 are a random per-process value plus a counter and carry no time.
Those 4 time bytes are unsigned, so ObjectId clocks run out on 2106-02-07 — not 2038: the year-2038 problem is a signed-32-bit bug, and this field never was one. An ID starting 00000000 decodes to 1970-01-01, which is what hand-written test fixtures usually do. NOW inserts a real current second and random bytes, so it is a shape sample, not an id Mongo assigned.
Twelve bytes, one of them a clock
| Bytes | Hex digits | Holds |
|---|---|---|
| 0–3 | first 8 | Unix seconds when the id was created, big-endian — the only part with time in it |
| 4–8 | next 10 | a random value generated once per process (MongoDB before 3.4: 3 bytes of machine identifier + 2 bytes of process id) |
| 9–11 | last 6 | a counter, initialized to a random value and incremented for each id in the same second |
- Whole seconds only. Two IDs generated 40 ms apart decode to the same second only if they do not cross a second boundary. ID generation time need not equal document insertion time. If your query needs sub-second ordering, the ObjectId is not the column to sort on — and the widget's millisecond row is that second padded to three zeros, nothing more.
- Timezone-free by construction. The 4 bytes are a Unix second count, so the instant is absolute; whether it prints as 19:34 or 03:34 is your client's setting. See Unix timestamp to date for the display half.
- Sorting by
_idis sorting by second, with a caveat. Inside one process the counter keeps ids in order, but two processes in the same second have unrelated random bytes — so_idordering across application instances is not a total time order. - Unsigned 32-bit clock. The field is not
time_t, so it does not hit the signed 2038 boundary: it overflows at 4,294,967,295 seconds, i.e. 2106-02-07 06:28:15 UTC. An id startingffffffffis the last representable second.
Reading the prefix without a converter
- mongosh / shell:
ObjectId("652f2a7c9b1c8f3d4e5a6b7c").getTimestamp()returns a JS Date.new Date(parseInt("652f2a7c", 16) * 1000)is the same thing without the driver. - Bash: turn the first 8 hex digits into a number, then feed it to
date:printf '%d\n' 0x652f2a7cgives 1697589884, anddate -u -d @1697589884(GNU) ordate -u -r 1697589884(BSD/macOS) prints 2023-10-18 00:44:44 UTC. Same instant, no converter. - Python:
ObjectId("652f2a7c9b1c8f3d4e5a6b7c").generation_timefrombsongives a timezone-aware UTCdatetime. - The reason anyone decodes this in production:
_idis indexed, so a date range expressed as ObjectIds is a cheap index scan.db.events.find({ _id: { $gte: ObjectId(hex_of_that_second + "0000000000000000") } })— prefix with the 8 hex digits of your lower bound, zero-pad the remaining 16, and you bound the embedded ID-generation second without acreated_atindex. This does not guarantee insertion-time filtering; IDs can be generated earlier, supplied manually or use skewed client clocks.
Ids copied out of a shell often arrive quoted ("652f2a7c…") — the widget strips surrounding quotes. Anything that is not 24 hex digits is not an ObjectId: a 36-character dashed string is a UUID, and versions 1, 6 and 7 have standard timestamp layouts (v8 may use a custom one) (see the UUID decoder). A Mongo Binary(UUID(...)) sub-type 4 field prints the same 32 hex digits without dashes, which is why the length check is worth reading rather than trusting.
ANSWERS
01 How do I get the date from a MongoDB ObjectId?
Take the first 8 hex characters, read them as a big-endian base-16 integer, and treat the result as Unix seconds: 0x652f2a7c → 1697589884 → 2023-10-18 00:44:44 UTC. The remaining 16 hex digits are random and a counter; they carry no time.
02 Can I recover sub-second creation time from an ObjectId?
No. The timestamp field is 4 bytes of whole seconds, so everything inside one second is indistinguishable. If you need millisecond ordering, store a separate created_at (or use UUIDv7, whose clock field is milliseconds).
03 Is the ObjectId timestamp UTC or local?
It is a Unix second count, which has no timezone — the instant is the same for everyone. What changes is rendering: mongosh prints the Date in the client's zone, so two terminals show different strings for one id.
04 How do I query documents created after a given date using _id?
Convert the cutoff to Unix seconds, print it as 8 hex digits, append 16 zeros, and compare: { _id: { $gte: ObjectId("652f2a7c0000000000000000") } }. This can use the default _id index, but filters the embedded ID-generation second, not necessarily the insertion time. Use an explicit indexed date field when insertion time is the requirement.
05 What happens to ObjectIds in 2038?
Nothing — the field is unsigned, so the Y2038 signed-32-bit cliff does not apply. ObjectId timestamps run to 4,294,967,295 seconds, which is 2106-02-07. Ids whose first 8 digits are 00000000 decode to 1970-01-01 and are almost always hand-written fixtures, not real documents.