How to Convert Twitter Snowflake ID to Timestamp
Twitter Snowflake IDs encode Unix timestamps in their first 41 bits. To extract the timestamp:
- Right-shift the Snowflake ID by 22 bits to extract the timestamp portion
- Add Twitter's epoch (1288834974657 milliseconds) to get Unix timestamp
- Divide by 1000 to convert milliseconds to seconds if needed
Conversion Formula
timestamp_ms = (snowflake_id >> 22) + 1288834974657
timestamp_sec = timestamp_ms / 1000
Where:
- snowflake_id: Twitter ID (64-bit integer)
- >> 22: Right-shift by 22 bits
- 1288834974657: Twitter epoch (Nov 4, 2010, 01:42:54 UTC)
- timestamp_ms: Unix timestamp in milliseconds
- timestamp_sec: Unix timestamp in seconds
Code Examples
function twitterIdToTimestamp(snowflakeId) {
const TWITTER_EPOCH = 1288834974657n;
const id = BigInt(snowflakeId);
const timestampMs = Number((id >> 22n) + TWITTER_EPOCH);
const timestampSec = Math.floor(timestampMs / 1000);
return { timestampMs, timestampSec };
}
// Example
const result = twitterIdToTimestamp('1382350606417817604');
console.log(result.timestampMs); // 1618414206657
console.log(result.timestampSec); // 1618414206
def twitter_id_to_timestamp(snowflake_id):
TWITTER_EPOCH = 1288834974657
timestamp_ms = ((int(snowflake_id) >> 22) + TWITTER_EPOCH)
timestamp_sec = timestamp_ms // 1000
return timestamp_ms, timestamp_sec
# Example
timestamp_ms, timestamp_sec = twitter_id_to_timestamp('1382350606417817604')
print(f"Milliseconds: {timestamp_ms}") # 1618414206657
print(f"Seconds: {timestamp_sec}") # 1618414206
Why Use Unix Timestamps?
- Universal format: Works across all programming languages and databases
- Easy comparison: Simple numeric comparison for sorting and filtering
- Database compatibility: Most databases store timestamps as Unix time
- API integration: Many APIs expect Unix timestamps for date parameters
- Time calculations: Easy to calculate time differences in seconds
Common Use Cases
Database Storage
Store tweet timestamps as Unix time for efficient querying and indexing.
API Integration
Convert Twitter IDs to timestamps for time-based API filtering.
Analytics
Analyze tweet timing patterns using Unix timestamps for calculations.