Extract Unix timestamps from Twitter IDs with millisecond precision
Twitter Snowflake IDs are 64-bit integers where the first 41 bits encode the timestamp. To extract the Unix timestamp:
Extraction Formula:
timestamp_ms = (snowflake_id >> 22) + 1288834974657
Steps:
Use timestamps for chronological sorting and timeline analysis of tweets.
Convert Twitter IDs to timestamps for database storage and API responses.
Verify exact tweet posting times for investigations and fact-checking.
// JavaScript - Extract Unix timestamp from Twitter Snowflake ID
function extractTimestamp(snowflakeId) {
const TWITTER_EPOCH = 1288834974657n;
const id = BigInt(snowflakeId);
const timestampMs = Number((id >> 22n) + TWITTER_EPOCH);
const timestampSec = Math.floor(timestampMs / 1000);
return {
milliseconds: timestampMs,
seconds: timestampSec,
date: new Date(timestampMs)
};
}
// Example
const result = extractTimestamp('1382350606417817604');
console.log(result.milliseconds); // 1618414206657
console.log(result.seconds); // 1618414206
console.log(result.date.toISOString()); // 2021-04-14T15:30:06.657Z
# Python - Extract Unix timestamp from Twitter Snowflake ID
from datetime import datetime
def extract_timestamp(snowflake_id):
TWITTER_EPOCH = 1288834974657
timestamp_ms = ((int(snowflake_id) >> 22) + TWITTER_EPOCH)
timestamp_sec = timestamp_ms // 1000
return {
'milliseconds': timestamp_ms,
'seconds': timestamp_sec,
'date': datetime.fromtimestamp(timestamp_sec)
}
# Example
result = extract_timestamp('1382350606417817604')
print(result['milliseconds']) # 1618414206657
print(result['seconds']) # 1618414206
print(result['date'].isoformat()) # 2021-04-14T15:30:06
Twitter uses a custom epoch of November 4, 2010, 01:42:54 UTC (1288834974657 milliseconds since Unix epoch). This custom epoch:
Millisecond Precision: Twitter Snowflake IDs store timestamps with millisecond precision, making them accurate to 1/1000th of a second.
Why it matters: This precision allows for:
Q: What's the difference between milliseconds and seconds timestamp?
A: Milliseconds timestamp includes 3 extra digits for sub-second precision. Divide by 1000 to convert to seconds. Twitter stores milliseconds for higher accuracy.
Q: Can I extract timestamps from deleted tweets?
A: Yes! The timestamp is encoded in the ID itself, so even if a tweet is deleted, you can still extract when it was originally posted.
Q: Are Twitter user IDs also Snowflake IDs?
A: Yes, Twitter user IDs use the same Snowflake format. Extract the timestamp to see when an account was created.