Skip to content
alpaca.tools

MongoDB ID to Timestamp

Loading MongoDB ID to Timestamp...
5.01 rating50 uses
Your rating for MongoDB ID to TimestampYour rating
Timestamp Tools

About MongoDB ID to Timestamp

Paste one ObjectId, a mongosh line, a JSON document, or log lines, and every 24-character ID in it is converted to its creation time in your time zone, UTC, ISO 8601, and Unix time. See the timestamp, random value, and counter inside each ID, sort many IDs by age and export them to CSV, build a mongosh query for a date range, and copy code for mongosh, JavaScript, Python, Java, or Go.

Mongo ID to timestamp: how the conversion works

A MongoDB ObjectId is 12 bytes written as 24 hex characters. The first 4 bytes, the first 8 characters, hold the Unix time in seconds when the ID was generated. Reading them as a hexadecimal number gives the timestamp directly, with no lookup or database access.

For 507f1f77bcf86cd799439011, the timestamp part is 507f1f77, which is 1350508407 in decimal. That is Wednesday, October 17, 2012 at 21:13:27 UTC. The tool shows this as a full date in your chosen time zone, in UTC, as ISO 8601, as Unix seconds and milliseconds, and as an age such as "13 years ago".

ObjectId to timestamp from JSON, queries, and logs

You don't need to clean the input first. The tool picks out every 24-character hex string, whether it is bare, wrapped as ObjectId("...") or ObjectId('...'), written as Extended JSON like {"$oid": "..."}, part of a URL, or inside a log line. Upper and lower case both work, and repeated IDs are listed once.

Values that are almost an ObjectId are flagged with the reason, such as "23 of 24 characters, 1 missing" or the non-hex letters they contain, so a bad copy and paste is easy to spot. With two or more IDs you get the oldest, the newest, the time between them, and a table you can sort by creation time.

MongoDB ObjectId to timestamp, byte by byte

Under each result the ObjectId is split into its three fields, with the decimal value of each:

  • Timestamp, 4 bytes: seconds since 1970-01-01 UTC, stored big-endian.
  • Random value, 5 bytes: chosen once per process by the driver or server, so IDs from the same process share it.
  • Counter, 3 bytes: starts at a random number and goes up by one for each new ID, which keeps IDs made in the same second unique.

Timestamp to ObjectId for date range queries

Because the time comes first, ObjectIds sort by creation time, and you can filter on _id instead of a date field. Choose a From and To time, or a quick range like Last 7 days, and the tool builds the smallest ObjectId for each second: the time in hex followed by 16 zeros.

The query uses $gte for the start and $lt for the end, so a range from midnight to midnight covers whole days without overlap. The largest ObjectId of the start second, ending in 16 f characters, is shown too for queries that need an inclusive upper bound.

MongoDB timestamp in mongosh, JavaScript, Python, Java, and Go

The code panel fills your ObjectId into short snippets: getTimestamp() and $toDate in mongosh, a no-library one-liner and the Node.js driver in JavaScript, generation_time in Python's bson package, getDate() in the Java driver, and Timestamp() in the Go driver.

MongoDB also has a BSON type called Timestamp, used internally for the replication oplog. It is not the same as an ObjectId's time or a Date. For creation or update times you store yourself, use the Date type.

FAQ

The first 8 hex characters of an ObjectId are the number of seconds since January 1, 1970 UTC at the moment the ID was made. Convert them from hex to decimal and you have a Unix timestamp: 507f1f77 is 1350508407, which is October 17, 2012 at 21:13:27 UTC. In mongosh, calling getTimestamp() on the ObjectId returns the same moment as an ISODate.

Usually, but not always. It is the time the ID was generated. Drivers create the ID just before the insert, so the two match in most apps. They differ if your code makes the ObjectId earlier, if old records were imported with new IDs, or if the ID was copied from another system. Updates never change it, so it can't tell you when a document was last modified.

To the second. An ObjectId stores whole seconds, so the milliseconds are always 000 and every ID created in the same second shows the same time. IDs from the same process in that second can still be ordered by the counter, the last 6 hex characters.

MongoDB stores and prints dates in UTC, so mongosh shows this example as ISODate with 2012-10-17T21:13:27.000Z. The tool shows that UTC value next to the same moment in the time zone picked at the top, which starts as your device's zone. Pick UTC there to match mongosh exactly.

Yes, as long as your _id values are ObjectIds. Set a From and To time under Timestamp to ObjectId and copy the query. It uses the smallest ObjectId of each second, 8 hex characters of time followed by 16 zeros, as $gte and $lt bounds on _id, so the default _id index serves it without a separate createdAt field.

The timestamp is 4 bytes, so it runs from 1970-01-01 00:00:00 UTC (00000000) to 2106-02-07 06:28:15 UTC (ffffffff). The query builder shows an error for times outside that span instead of wrapping around.