MongoDB ObjectId to Timestamp Converter
See when any MongoDB document was created from its _id, or turn a date into an ObjectId for fast range queries.
How to get the timestamp from a MongoDB ObjectId
- Paste one or more ObjectIds, one per line. You can paste them straight from logs or mongosh, such as
ObjectId("65f1…"). - Read the creation time in UTC and in your local time zone.
- To query by date, pick a date on the right and copy the generated query.
How an ObjectId stores time
An ObjectId is 12 bytes, written as 24 hexadecimal characters. The first 4 bytes are the creation time in seconds since 1 January 1970 (UTC). The next 5 bytes are a random value, and the last 3 bytes are a counter.
| Characters | Meaning | Example from 65f1c2a9e4b0a1b2c3d4e5f6 |
|---|---|---|
| 1–8 | Timestamp in seconds | 65f1c2a9 = 13 March 2024, 15:13:45 UTC |
| 9–18 | Random value | e4b0a1b2c3 |
| 19–24 | Counter | d4e5f6 |
In code you rarely need to do this by hand:
// mongosh
ObjectId("65f1c2a9e4b0a1b2c3d4e5f6").getTimestamp()
// Node.js with Mongoose
const created = doc._id.getTimestamp(); // a JavaScript Date
Query documents by date using _id
Because the timestamp comes first, ObjectIds sort by creation time. That means you can find documents created after a date with the _id index alone, even if the collection has no createdAt field. Build the smallest ObjectId for that second with the date converter above, then query with $gte.
Two limits to keep in mind: the timestamp only has one-second precision, and it shows when the ObjectId was generated, which is usually but not always the moment the document was inserted.
Frequently asked questions
Do I still need a createdAt field?
Often you don’t, because the _id already holds the creation time. Add timestamps: true to your Mongoose schema when you also need updatedAt, millisecond precision, or easier sorting and display.
Is the time in an ObjectId UTC?
Yes. It counts seconds since the Unix epoch, which is always UTC. The tool also shows it in your computer’s local time zone.
Can two ObjectIds be the same?
In practice, no. The random value and the counter make each ObjectId unique even when many are created in the same second.
More free tools
-
JSON to Mongoose Schema Converter
Paste a JSON document and get a ready-to-use Mongoose schema and model, with nested schemas, ObjectId refs and timestamps.
-
JSON to Zod Schema Generator
Turn any JSON into a Zod schema plus its TypeScript type, ready to validate API responses and form data.
-
JSON to TypeScript Interface Generator
Generate clean TypeScript interfaces from JSON in one paste, including nested objects, arrays and optional fields.
-
SQL to MongoDB Query Converter
Convert SQL queries to MongoDB find(), aggregate(), updateMany() and more, for mongosh or Mongoose.
-
PAN, Aadhaar, GST and IFSC Validator
Check PAN, Aadhaar, GSTIN, IFSC, PIN code, mobile and UPI formats, with real checksum tests and copy-ready regex.
-
MongoDB Connection String Checker and Builder
Find what’s wrong with a MongoDB URI, from unencoded passwords to SRV ports, or build a correct one for Atlas or a local server.
