MongoDB ObjectId to timestamp
For example, 507f1f77bcf86cd799439011 is Wed, Oct 17, 2012, 09:13:27 PM UTC, which is 2012-10-17T21:13:27.000Z in ISO 8601 and 1350508407 in Unix seconds.
First 4 bytes read as seconds since the Unix epoch: the ObjectId's creation time (whole seconds only).
Detected: MongoDB ObjectId/t/507f1f77bcf86cd799439011
Your timezone
UTC · GMT+00:00 · UTC
Wed, Oct 17, 2012, 09:13:27 PM
Common formats
UTC
Wed, Oct 17, 2012, 09:13:27 PM
ISO 8601
2012-10-17T21:13:27.000Z
Unix (seconds)
1350508407
Unix (millis)
1350508407000
Relative
…
Every MongoDB ObjectId begins with its own creation time: the first 4 of its 12 bytes are a Unix timestamp in seconds. That means any document whose _id is an ObjectId carries a free created-at field. Paste one below to read it.
Do it with the API
curl https://parsetime.dev/api/t/507f1f77bcf86cd799439011?format=objectidThe same conversion as JSON: no key, no signup, permissive CORS. Try this call or read the API docs.
Common questions
- How do I get the timestamp from an ObjectId in the mongo shell?
- ObjectId("507f1f77bcf86cd799439011").getTimestamp() returns it as an ISODate. Drivers expose the same thing: bson.ObjectId.generation_time in Python, ObjectId.getTimestamp() in Node.
- Can I rely on ObjectId time as a created-at field?
- Mostly. It's assigned by whichever client generated the id, so clock skew between app servers shows up in it, and it only has whole-second precision. For anything that matters, store an explicit createdAt. For debugging and back-of-envelope queries, though, it's excellent.
- Can I query MongoDB by date range using _id?
- Yes. Construct an ObjectId whose first 4 bytes encode your boundary timestamp and the rest are zeros, then use it in $gte/$lt comparisons. This is a standard trick for time-range scans without a separate indexed date field.