Last updated
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
Twitter Snowflake to Timestamp Converter — Examples
The Twitter Snowflake to Timestamp Converter converts Twitter IDs to Unix timestamps and other timestamp formats. Here are complete examples for all common use cases.
ID to Unix Timestamp
// JavaScript — Snowflake ID to Unix timestamp
const TWITTER_EPOCH = 1288834974657n;
function snowflakeToUnixTimestamp(idStr) {
const id = BigInt(idStr);
const timestampMs = (id >> 22n) + TWITTER_EPOCH;
return {
ms: Number(timestampMs),
seconds: Math.floor(Number(timestampMs) / 1000)
};
}
const result = snowflakeToUnixTimestamp("1529877576591609861");
console.log("Unix (ms):", result.ms);
console.log("Unix (s):", result.seconds);
# Python — Snowflake ID to Unix timestamp
TWITTER_EPOCH_MS = 1288834974657
def snowflake_to_unix(id_str):
id_val = int(id_str)
ts_ms = (id_val >> 22) + TWITTER_EPOCH_MS
return {
"ms": ts_ms,
"seconds": ts_ms // 1000,
"float_seconds": ts_ms / 1000
}
result = snowflake_to_unix("1529877576591609861")
print(f"Unix (ms): {result['ms']}")
print(f"Unix (s): {result['seconds']}")
All Timestamp Formats
// Produce all common timestamp formats from a Snowflake ID
function allTimestampFormats(idStr) {
const id = BigInt(idStr);
const TWITTER_EPOCH = 1288834974657n;
const tsMs = (id >> 22n) + TWITTER_EPOCH;
const date = new Date(Number(tsMs));
return {
"Unix (ms)": Number(tsMs),
"Unix (s)": Math.floor(Number(tsMs) / 1000),
"ISO 8601": date.toISOString(),
"UTC string": date.toUTCString(),
"RFC 2822": date.toUTCString(),
"Date only": date.toISOString().split("T")[0],
"Time only": date.toISOString().split("T")[1].replace("Z", " UTC")
};
}
const formats = allTimestampFormats("1529877576591609861");
Object.entries(formats).forEach(([fmt, val]) => {
console.log(`${fmt.padEnd(14)}: ${val}`);
});
Reverse: Unix Timestamp to Snowflake ID
// Convert a Unix timestamp to the minimum Snowflake ID for that moment
function unixToSnowflakeId(unixSeconds) {
const TWITTER_EPOCH = 1288834974657n;
const tsMs = BigInt(unixSeconds * 1000) - TWITTER_EPOCH;
if (tsMs < 0n) {
throw new Error("Timestamp is before Twitter's Snowflake epoch");
}
return (tsMs << 22n).toString();
}
// Example: get the min ID for a Unix timestamp
const unixTs = 1672531200; // 2023-01-01 00:00:00 UTC
const minId = unixToSnowflakeId(unixTs);
console.log(`Min ID for Unix ${unixTs}: ${minId}`);
# Python — Unix timestamp to Snowflake ID
TWITTER_EPOCH_MS = 1288834974657
def unix_to_snowflake_id(unix_seconds):
"""Convert a Unix timestamp (seconds) to the minimum Snowflake ID."""
ts_ms = int(unix_seconds * 1000) - TWITTER_EPOCH_MS
if ts_ms < 0:
raise ValueError("Timestamp is before Twitter's Snowflake epoch")
return str(ts_ms << 22)
# Example
import time
now = int(time.time())
min_id = unix_to_snowflake_id(now)
print(f"Min ID for current time: {min_id}")
Using Timestamps in Twitter API Queries
# Python — use Snowflake IDs for efficient API pagination
import requests
from datetime import datetime, timezone
TWITTER_EPOCH_MS = 1288834974657
def date_to_min_snowflake_id(dt):
ts_ms = int(dt.timestamp() * 1000) - TWITTER_EPOCH_MS
return str(ts_ms << 22)
def fetch_tweets_since(query, since_date, bearer_token):
since_id = date_to_min_snowflake_id(since_date)
headers = {"Authorization": f"Bearer {bearer_token}"}
params = {
"query": query,
"since_id": since_id,
"max_results": 100,
"tweet.fields": "created_at,author_id,text"
}
response = requests.get(
"https://api.twitter.com/2/tweets/search/recent",
headers=headers,
params=params
)
return response.json()
# Fetch tweets from the last 7 days
from datetime import timedelta
since = datetime.now(timezone.utc) - timedelta(days=7)
data = fetch_tweets_since("#python", since, "YOUR_TOKEN")
Batch Conversion to Unix Timestamps
# Python — batch convert IDs to Unix timestamps
import csv
TWITTER_EPOCH_MS = 1288834974657
def batch_to_unix(id_list):
results = []
for id_str in id_list:
id_val = int(id_str.strip())
ts_ms = (id_val >> 22) + TWITTER_EPOCH_MS
results.append({
"id": id_str,
"unix_ms": ts_ms,
"unix_s": ts_ms // 1000
})
return results
ids = [
"1529877576591609861",
"1700000000000000000",
"1800000000000000000",
]
results = batch_to_unix(ids)
# Export to CSV
with open("unix_timestamps.csv", "w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=["id", "unix_ms", "unix_s"])
writer.writeheader()
writer.writerows(results)
print(f"Exported {len(results)} rows")
Timestamp Format Reference
- Unix (ms):
1653572200000— use with JavaScript Date, Java Instant.ofEpochMilli() - Unix (s):
1653572200— use with Python datetime.fromtimestamp(), most databases - ISO 8601:
2022-05-26T14:30:00.000Z— use in APIs and data exchange - RFC 2822:
Thu, 26 May 2022 14:30:00 GMT— use in HTTP headers and email
The converter produces all these formats from a single Snowflake ID, making it easy to use the timestamp in any context — from database storage to API responses to human-readable display.
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.
Frequently Asked Questions
Paste the Twitter ID into our converter and click 'Convert to Timestamp'. You'll get the Unix timestamp in milliseconds and seconds. Twitter IDs encode timestamps using epoch November 4, 2010, 01:42:54 UTC (1288834974657ms).
Formula: timestamp_ms = (snowflake_id >> 22) + 1288834974657. Right-shift the ID by 22 bits to extract the 41-bit timestamp, then add Twitter's epoch (1288834974657) to get Unix timestamp in milliseconds.