Timestamp Resources
Timestamp Format Cheatsheet: Quick Reference Guide
Overview
This comprehensive cheatsheet provides quick reference for all common timestamp formats used in programming and data exchange. All examples use January 1, 2024, 12:00:00 UTC as the reference time.
Quick Reference Table
| Format | Example | Length | Use Case |
|---|---|---|---|
| Unix Seconds | 1704110400 | 10 digits | Legacy systems, APIs |
| Unix Milliseconds | 1704110400000 | 13 digits | JavaScript, Java, Modern APIs |
| Unix Microseconds | 1704110400000000 | 16 digits | High-precision logging |
| Unix Nanoseconds | 1704110400000000000 | 19 digits | Performance monitoring |
| ISO 8601 (UTC) | 2024-01-01T12:00:00Z | Variable | International standard |
| ISO 8601 (Timezone) | 2024-01-01T12:00:00+00:00 | Variable | Timezone-aware data |
| RFC 3339 | 2024-01-01T12:00:00.000Z | Variable | Internet timestamps |
| RFC 2822 | Mon, 01 Jan 2024 12:00:00 +0000 | Variable | Email headers |
| SQL DATETIME | 2024-01-01 12:00:00 | 19 chars | Database storage |
Unix Timestamp Formats
Seconds (10 digits)
Format: Integer representing seconds since January 1, 1970, 00:00:00 UTC
1704110400
When to Use:
- Legacy systems and APIs
- Storage efficiency (4 bytes as int32)
- Year 2038 problem considerations
Code Examples:
// JavaScript
const timestamp = Math.floor(Date.now() / 1000);
// 1704110400
const date = new Date(timestamp * 1000);
// 2024-01-01T12:00:00.000Z
# Python
import time
timestamp = int(time.time())
# 1704110400
from datetime import datetime
date = datetime.fromtimestamp(timestamp)
# 2024-01-01 12:00:00
// PHP
$timestamp = time();
// 1704110400
$date = date('Y-m-d H:i:s', $timestamp);
// 2024-01-01 12:00:00
// Go
import "time"
timestamp := time.Now().Unix()
// 1704110400
date := time.Unix(timestamp, 0)
// 2024-01-01 12:00:00 +0000 UTC
Milliseconds (13 digits)
Format: Integer representing milliseconds since Unix epoch
1704110400000
When to Use:
- JavaScript applications (default for
Date.now()) - Modern web APIs
- Millisecond precision needed
Code Examples:
// JavaScript
const timestamp = Date.now();
// 1704110400000
const date = new Date(timestamp);
// 2024-01-01T12:00:00.000Z
# Python
import time
timestamp = int(time.time() * 1000)
# 1704110400000
from datetime import datetime
date = datetime.fromtimestamp(timestamp / 1000)
// Java
long timestamp = System.currentTimeMillis();
// 1704110400000
Date date = new Date(timestamp);
// Mon Jan 01 12:00:00 UTC 2024
// C#
long timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
// 1704110400000
DateTimeOffset date = DateTimeOffset.FromUnixTimeMilliseconds(timestamp);
Microseconds (16 digits)
Format: Integer representing microseconds since Unix epoch
1704110400000000
When to Use:
- High-precision logging and monitoring
- Performance benchmarking
- Scientific applications
Code Examples:
# Python
import time
timestamp = int(time.time() * 1_000_000)
# 1704110400000000
date = datetime.fromtimestamp(timestamp / 1_000_000)
// Go
timestamp := time.Now().UnixMicro()
// 1704110400000000
date := time.UnixMicro(timestamp)
// Rust
use std::time::{SystemTime, UNIX_EPOCH};
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_micros();
// 1704110400000000
Nanoseconds (19 digits)
Format: Integer representing nanoseconds since Unix epoch
1704110400000000000
When to Use:
- Ultra-precise timing requirements
- Performance profiling
- Trading systems
Code Examples:
// Go
timestamp := time.Now().UnixNano()
// 1704110400000000000
date := time.Unix(0, timestamp)
// Rust
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
// 1704110400000000000
ISO 8601 Formats
Basic Format (UTC)
Format: YYYY-MM-DDTHH:MM:SSZ
2024-01-01T12:00:00Z
When to Use:
- International data exchange
- RESTful APIs
- JSON data storage
Code Examples:
// JavaScript
const isoString = new Date().toISOString();
// 2024-01-01T12:00:00.000Z
# Python
from datetime import datetime, timezone
iso_string = datetime.now(timezone.utc).isoformat().replace('+00:00', 'Z')
# 2024-01-01T12:00:00Z
// PHP
$iso_string = gmdate('Y-m-d\TH:i:s\Z');
// 2024-01-01T12:00:00Z
Extended Format (with milliseconds)
Format: YYYY-MM-DDTHH:MM:SS.sssZ
2024-01-01T12:00:00.000Z
// JavaScript (default)
new Date().toISOString();
// 2024-01-01T12:00:00.000Z
# Python
datetime.now(timezone.utc).isoformat(timespec='milliseconds').replace('+00:00', 'Z')
# 2024-01-01T12:00:00.000Z
With Timezone Offset
Format: YYYY-MM-DDTHH:MM:SS±HH:MM
2024-01-01T12:00:00+00:00
2024-01-01T17:30:00+05:30 # IST
2024-01-01T07:00:00-05:00 # EST
Code Examples:
// JavaScript
const date = new Date();
const offset = -date.getTimezoneOffset();
const sign = offset >= 0 ? '+' : '-';
const hours = String(Math.floor(Math.abs(offset) / 60)).padStart(2, '0');
const minutes = String(Math.abs(offset) % 60).padStart(2, '0');
const iso = date.toISOString().replace('Z', `${sign}${hours}:${minutes}`);
// 2024-01-01T12:00:00.000+00:00
# Python
datetime.now(timezone.utc).isoformat()
# 2024-01-01T12:00:00+00:00
# With specific timezone
from datetime import timezone, timedelta
ist = timezone(timedelta(hours=5, minutes=30))
datetime.now(ist).isoformat()
# 2024-01-01T17:30:00+05:30
Date Only
Format: YYYY-MM-DD
2024-01-01
// JavaScript
new Date().toISOString().split('T')[0];
// 2024-01-01
# Python
datetime.now().date().isoformat()
# 2024-01-01
Time Only
Format: HH:MM:SS or HH:MM:SS.sss
12:00:00
12:00:00.000
// JavaScript
new Date().toISOString().split('T')[1];
// 12:00:00.000Z
# Python
datetime.now().time().isoformat()
# 12:00:00.000000
RFC 3339 Format
Format: YYYY-MM-DDTHH:MM:SS.sss±HH:MM or ...Z
2024-01-01T12:00:00.000Z
2024-01-01T12:00:00.000+00:00
Difference from ISO 8601:
- Always includes milliseconds
- Always includes timezone
- More strict specification
Code Examples:
// JavaScript
new Date().toISOString(); // Already RFC 3339 compliant
// 2024-01-01T12:00:00.000Z
# Python
from datetime import datetime, timezone
datetime.now(timezone.utc).isoformat()
# 2024-01-01T12:00:00.000000+00:00
// Go
time.Now().UTC().Format(time.RFC3339Nano)
// 2024-01-01T12:00:00.000000000Z
RFC 2822 Format
Format: Day, DD Mon YYYY HH:MM:SS ±HHMM
Mon, 01 Jan 2024 12:00:00 +0000
Mon, 01 Jan 2024 12:00:00 GMT
When to Use:
- Email headers (Date field)
- HTTP headers
- RSS feeds
Code Examples:
// JavaScript
new Date().toUTCString();
// Mon, 01 Jan 2024 12:00:00 GMT
# Python
from email.utils import formatdate
formatdate(timeval=None, localtime=False, usegmt=True)
# Mon, 01 Jan 2024 12:00:00 GMT
// PHP
date('r');
// Mon, 01 Jan 2024 12:00:00 +0000
SQL Timestamp Formats
MySQL/MariaDB DATETIME
Format: YYYY-MM-DD HH:MM:SS
2024-01-01 12:00:00
-- MySQL
SELECT NOW();
-- 2024-01-01 12:00:00
SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s');
-- 2024-01-01 12:00:00
PostgreSQL TIMESTAMP
Format: YYYY-MM-DD HH:MM:SS.ssssss±TZ
2024-01-01 12:00:00.000000+00
2024-01-01 12:00:00+00
-- PostgreSQL
SELECT NOW();
-- 2024-01-01 12:00:00.000000+00
SELECT CURRENT_TIMESTAMP;
-- 2024-01-01 12:00:00.000000+00
SQLite DATETIME
Format: YYYY-MM-DD HH:MM:SS (stored as text)
2024-01-01 12:00:00
-- SQLite
SELECT datetime('now');
-- 2024-01-01 12:00:00
SELECT strftime('%Y-%m-%d %H:%M:%S', 'now');
-- 2024-01-01 12:00:00
Microsoft SQL Server
Format: YYYY-MM-DD HH:MM:SS.sss
2024-01-01 12:00:00.000
-- SQL Server
SELECT GETUTCDATE();
-- 2024-01-01 12:00:00.000
SELECT FORMAT(GETUTCDATE(), 'yyyy-MM-dd HH:mm:ss.fff');
-- 2024-01-01 12:00:00.000
Language-Specific Formats
JavaScript
// Various formats
const date = new Date();
// ISO 8601
date.toISOString(); // 2024-01-01T12:00:00.000Z
// Locale string
date.toLocaleString(); // 1/1/2024, 12:00:00 PM
// UTC string
date.toUTCString(); // Mon, 01 Jan 2024 12:00:00 GMT
// Date only
date.toLocaleDateString(); // 1/1/2024
// Time only
date.toLocaleTimeString(); // 12:00:00 PM
// Custom format
date.toLocaleString('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
});
// 01/01/2024, 12:00:00
Python strftime Codes
from datetime import datetime
dt = datetime(2024, 1, 1, 12, 0, 0)
# Common formats
dt.strftime('%Y-%m-%d') # 2024-01-01
dt.strftime('%Y-%m-%d %H:%M:%S') # 2024-01-01 12:00:00
dt.strftime('%Y-%m-%dT%H:%M:%SZ') # 2024-01-01T12:00:00Z
dt.strftime('%d/%m/%Y') # 01/01/2024
dt.strftime('%m/%d/%Y') # 01/01/2024
dt.strftime('%B %d, %Y') # January 01, 2024
dt.strftime('%A, %B %d, %Y') # Monday, January 01, 2024
dt.strftime('%I:%M %p') # 12:00 PM
PHP date() Format
<?php
$timestamp = 1704110400;
// Common formats
date('Y-m-d', $timestamp); // 2024-01-01
date('Y-m-d H:i:s', $timestamp); // 2024-01-01 12:00:00
date('c', $timestamp); // 2024-01-01T12:00:00+00:00 (ISO 8601)
date('r', $timestamp); // Mon, 01 Jan 2024 12:00:00 +0000 (RFC 2822)
date('U', $timestamp); // 1704110400 (Unix timestamp)
date('d/m/Y', $timestamp); // 01/01/2024
date('m/d/Y', $timestamp); // 01/01/2024
date('F j, Y', $timestamp); // January 1, 2024
date('l, F j, Y', $timestamp); // Monday, January 1, 2024
date('g:i A', $timestamp); // 12:00 PM
Go Time Format
package main
import (
"time"
)
func main() {
t := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)
// Standard formats
t.Format(time.RFC3339) // 2024-01-01T12:00:00Z
t.Format(time.RFC3339Nano) // 2024-01-01T12:00:00.000000000Z
t.Format(time.RFC822) // 01 Jan 24 12:00 UTC
t.Format(time.RFC1123) // Mon, 01 Jan 2024 12:00:00 UTC
// Custom formats (using reference time: Jan 2 15:04:05 2006 MST)
t.Format("2006-01-02") // 2024-01-01
t.Format("2006-01-02 15:04:05") // 2024-01-01 12:00:00
t.Format("01/02/2006") // 01/01/2024
t.Format("January 2, 2006") // January 1, 2024
t.Format("3:04 PM") // 12:00 PM
}
Java SimpleDateFormat
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
Date date = new Date(1704110400000L);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
// Common patterns
"yyyy-MM-dd" // 2024-01-01
"yyyy-MM-dd HH:mm:ss" // 2024-01-01 12:00:00
"yyyy-MM-dd'T'HH:mm:ss'Z'" // 2024-01-01T12:00:00Z
"dd/MM/yyyy" // 01/01/2024
"MM/dd/yyyy" // 01/01/2024
"MMMM d, yyyy" // January 1, 2024
"EEEE, MMMM d, yyyy" // Monday, January 1, 2024
"h:mm a" // 12:00 PM
Format Conversion Quick Reference
Unix → ISO 8601
// JavaScript
new Date(1704110400000).toISOString();
// 2024-01-01T12:00:00.000Z
# Python
from datetime import datetime, timezone
datetime.fromtimestamp(1704110400, timezone.utc).isoformat()
# 2024-01-01T12:00:00+00:00
ISO 8601 → Unix
// JavaScript
new Date('2024-01-01T12:00:00Z').getTime();
// 1704110400000
# Python
from datetime import datetime
datetime.fromisoformat('2024-01-01T12:00:00+00:00').timestamp()
# 1704110400.0
RFC 2822 → ISO 8601
// JavaScript
new Date('Mon, 01 Jan 2024 12:00:00 GMT').toISOString();
// 2024-01-01T12:00:00.000Z
# Python
from email.utils import parsedate_to_datetime
parsedate_to_datetime('Mon, 01 Jan 2024 12:00:00 GMT').isoformat()
# 2024-01-01T12:00:00+00:00
Best Practices
Storage Recommendations
- Databases: Use native TIMESTAMP type with timezone support
- APIs: Use ISO 8601 with timezone for portability
- Internal Processing: Use Unix milliseconds for calculations
- Logging: Use ISO 8601 with milliseconds and timezone
Timezone Handling
- Always include timezone - Avoid ambiguous timestamps
- Store in UTC - Convert to local time only for display
- Use Z for UTC - Clearer than +00:00
- Validate timezone offsets - Ensure they're within -12:00 to +14:00
Precision Selection
| Precision | Use Case | Example |
|---|---|---|
| Seconds | User events, scheduling | 1704110400 |
| Milliseconds | Web apps, general logging | 1704110400000 |
| Microseconds | Performance monitoring | 1704110400000000 |
| Nanoseconds | High-frequency trading | 1704110400000000000 |
Common Pitfalls
❌ Avoid:
- Storing timestamps without timezone information
- Using local time for storage (always use UTC)
- Mixing different precision levels
- Hardcoding timezone offsets
- Using 32-bit integers (Year 2038 problem)
✅ Do:
- Always include timezone designators (Z or ±HH:MM)
- Store in UTC, convert to local time for display only
- Use consistent precision across your system
- Use timezone-aware datetime objects
- Use 64-bit integers for Unix timestamps
Related Tools
- Unix Timestamp Converter - Convert between formats
- Timestamp Format Builder - Create custom formats
- Timezone Converter - Handle timezone conversions
- Batch Timestamp Converter - Convert multiple timestamps