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:
JAVASCRIPT1// JavaScript 2const timestamp = Math.floor(Date.now() / 1000); 3// 1704110400 4 5const date = new Date(timestamp * 1000); 6// 2024-01-01T12:00:00.000Z
PYTHON1# Python 2import time 3timestamp = int(time.time()) 4# 1704110400 5 6from datetime import datetime 7date = datetime.fromtimestamp(timestamp) 8# 2024-01-01 12:00:00
PHP1// PHP 2$timestamp = time(); 3// 1704110400 4 5$date = date('Y-m-d H:i:s', $timestamp); 6// 2024-01-01 12:00:00
GO1// Go 2import "time" 3 4timestamp := time.Now().Unix() 5// 1704110400 6 7date := time.Unix(timestamp, 0) 8// 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:
JAVASCRIPT1// JavaScript 2const timestamp = Date.now(); 3// 1704110400000 4 5const date = new Date(timestamp); 6// 2024-01-01T12:00:00.000Z
PYTHON1# Python 2import time 3timestamp = int(time.time() * 1000) 4# 1704110400000 5 6from datetime import datetime 7date = datetime.fromtimestamp(timestamp / 1000)
JAVA1// Java 2long timestamp = System.currentTimeMillis(); 3// 1704110400000 4 5Date date = new Date(timestamp); 6// Mon Jan 01 12:00:00 UTC 2024
CSHARP1// C# 2long timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); 3// 1704110400000 4 5DateTimeOffset 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:
PYTHON1# Python 2import time 3timestamp = int(time.time() * 1_000_000) 4# 1704110400000000 5 6date = datetime.fromtimestamp(timestamp / 1_000_000)
GO1// Go 2timestamp := time.Now().UnixMicro() 3// 1704110400000000 4 5date := time.UnixMicro(timestamp)
RUST1// Rust 2use std::time::{SystemTime, UNIX_EPOCH}; 3 4let timestamp = SystemTime::now() 5 .duration_since(UNIX_EPOCH) 6 .unwrap() 7 .as_micros(); 8// 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:
GO1// Go 2timestamp := time.Now().UnixNano() 3// 1704110400000000000 4 5date := time.Unix(0, timestamp)
RUST1// Rust 2let timestamp = SystemTime::now() 3 .duration_since(UNIX_EPOCH) 4 .unwrap() 5 .as_nanos(); 6// 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:
JAVASCRIPT1// JavaScript 2const isoString = new Date().toISOString(); 3// 2024-01-01T12:00:00.000Z
PYTHON1# Python 2from datetime import datetime, timezone 3iso_string = datetime.now(timezone.utc).isoformat().replace('+00:00', 'Z') 4# 2024-01-01T12:00:00Z
PHP1// PHP 2$iso_string = gmdate('Y-m-d\TH:i:s\Z'); 3// 2024-01-01T12:00:00Z
Extended Format (with milliseconds)
Format: YYYY-MM-DDTHH:MM:SS.sssZ
2024-01-01T12:00:00.000Z
JAVASCRIPT1// JavaScript (default) 2new Date().toISOString(); 3// 2024-01-01T12:00:00.000Z
PYTHON1# Python 2datetime.now(timezone.utc).isoformat(timespec='milliseconds').replace('+00:00', 'Z') 3# 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:
JAVASCRIPT1// JavaScript 2const date = new Date(); 3const offset = -date.getTimezoneOffset(); 4const sign = offset >= 0 ? '+' : '-'; 5const hours = String(Math.floor(Math.abs(offset) / 60)).padStart(2, '0'); 6const minutes = String(Math.abs(offset) % 60).padStart(2, '0'); 7const iso = date.toISOString().replace('Z', `${sign}${hours}:${minutes}`); 8// 2024-01-01T12:00:00.000+00:00
PYTHON1# Python 2datetime.now(timezone.utc).isoformat() 3# 2024-01-01T12:00:00+00:00 4 5# With specific timezone 6from datetime import timezone, timedelta 7ist = timezone(timedelta(hours=5, minutes=30)) 8datetime.now(ist).isoformat() 9# 2024-01-01T17:30:00+05:30
Date Only
Format: YYYY-MM-DD
2024-01-01
JAVASCRIPT1// JavaScript 2new Date().toISOString().split('T')[0]; 3// 2024-01-01
PYTHON1# Python 2datetime.now().date().isoformat() 3# 2024-01-01
Time Only
Format: HH:MM:SS or HH:MM:SS.sss
12:00:00
12:00:00.000
JAVASCRIPT1// JavaScript 2new Date().toISOString().split('T')[1]; 3// 12:00:00.000Z
PYTHON1# Python 2datetime.now().time().isoformat() 3# 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:
JAVASCRIPT1// JavaScript 2new Date().toISOString(); // Already RFC 3339 compliant 3// 2024-01-01T12:00:00.000Z
PYTHON1# Python 2from datetime import datetime, timezone 3datetime.now(timezone.utc).isoformat() 4# 2024-01-01T12:00:00.000000+00:00
GO1// Go 2time.Now().UTC().Format(time.RFC3339Nano) 3// 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:
JAVASCRIPT1// JavaScript 2new Date().toUTCString(); 3// Mon, 01 Jan 2024 12:00:00 GMT
PYTHON1# Python 2from email.utils import formatdate 3formatdate(timeval=None, localtime=False, usegmt=True) 4# Mon, 01 Jan 2024 12:00:00 GMT
PHP1// PHP 2date('r'); 3// 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
SQL1-- MySQL 2SELECT NOW(); 3-- 2024-01-01 12:00:00 4 5SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s'); 6-- 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
SQL1-- PostgreSQL 2SELECT NOW(); 3-- 2024-01-01 12:00:00.000000+00 4 5SELECT CURRENT_TIMESTAMP; 6-- 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
SQL1-- SQLite 2SELECT datetime('now'); 3-- 2024-01-01 12:00:00 4 5SELECT strftime('%Y-%m-%d %H:%M:%S', 'now'); 6-- 2024-01-01 12:00:00
Microsoft SQL Server
Format: YYYY-MM-DD HH:MM:SS.sss
2024-01-01 12:00:00.000
SQL1-- SQL Server 2SELECT GETUTCDATE(); 3-- 2024-01-01 12:00:00.000 4 5SELECT FORMAT(GETUTCDATE(), 'yyyy-MM-dd HH:mm:ss.fff'); 6-- 2024-01-01 12:00:00.000
Language-Specific Formats
JavaScript
JAVASCRIPT1// Various formats 2const date = new Date(); 3 4// ISO 8601 5date.toISOString(); // 2024-01-01T12:00:00.000Z 6 7// Locale string 8date.toLocaleString(); // 1/1/2024, 12:00:00 PM 9 10// UTC string 11date.toUTCString(); // Mon, 01 Jan 2024 12:00:00 GMT 12 13// Date only 14date.toLocaleDateString(); // 1/1/2024 15 16// Time only 17date.toLocaleTimeString(); // 12:00:00 PM 18 19// Custom format 20date.toLocaleString('en-US', { 21 year: 'numeric', 22 month: '2-digit', 23 day: '2-digit', 24 hour: '2-digit', 25 minute: '2-digit', 26 second: '2-digit', 27 hour12: false 28}); 29// 01/01/2024, 12:00:00
Python strftime Codes
PYTHON1from datetime import datetime 2 3dt = datetime(2024, 1, 1, 12, 0, 0) 4 5# Common formats 6dt.strftime('%Y-%m-%d') # 2024-01-01 7dt.strftime('%Y-%m-%d %H:%M:%S') # 2024-01-01 12:00:00 8dt.strftime('%Y-%m-%dT%H:%M:%SZ') # 2024-01-01T12:00:00Z 9dt.strftime('%d/%m/%Y') # 01/01/2024 10dt.strftime('%m/%d/%Y') # 01/01/2024 11dt.strftime('%B %d, %Y') # January 01, 2024 12dt.strftime('%A, %B %d, %Y') # Monday, January 01, 2024 13dt.strftime('%I:%M %p') # 12:00 PM
PHP date() Format
PHP1<?php 2$timestamp = 1704110400; 3 4// Common formats 5date('Y-m-d', $timestamp); // 2024-01-01 6date('Y-m-d H:i:s', $timestamp); // 2024-01-01 12:00:00 7date('c', $timestamp); // 2024-01-01T12:00:00+00:00 (ISO 8601) 8date('r', $timestamp); // Mon, 01 Jan 2024 12:00:00 +0000 (RFC 2822) 9date('U', $timestamp); // 1704110400 (Unix timestamp) 10date('d/m/Y', $timestamp); // 01/01/2024 11date('m/d/Y', $timestamp); // 01/01/2024 12date('F j, Y', $timestamp); // January 1, 2024 13date('l, F j, Y', $timestamp); // Monday, January 1, 2024 14date('g:i A', $timestamp); // 12:00 PM
Go Time Format
GO1package main 2 3import ( 4 "time" 5) 6 7func main() { 8 t := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC) 9 10 // Standard formats 11 t.Format(time.RFC3339) // 2024-01-01T12:00:00Z 12 t.Format(time.RFC3339Nano) // 2024-01-01T12:00:00.000000000Z 13 t.Format(time.RFC822) // 01 Jan 24 12:00 UTC 14 t.Format(time.RFC1123) // Mon, 01 Jan 2024 12:00:00 UTC 15 16 // Custom formats (using reference time: Jan 2 15:04:05 2006 MST) 17 t.Format("2006-01-02") // 2024-01-01 18 t.Format("2006-01-02 15:04:05") // 2024-01-01 12:00:00 19 t.Format("01/02/2006") // 01/01/2024 20 t.Format("January 2, 2006") // January 1, 2024 21 t.Format("3:04 PM") // 12:00 PM 22}
Java SimpleDateFormat
JAVA1import java.text.SimpleDateFormat; 2import java.util.Date; 3import java.util.TimeZone; 4 5Date date = new Date(1704110400000L); 6SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); 7sdf.setTimeZone(TimeZone.getTimeZone("UTC")); 8 9// Common patterns 10"yyyy-MM-dd" // 2024-01-01 11"yyyy-MM-dd HH:mm:ss" // 2024-01-01 12:00:00 12"yyyy-MM-dd'T'HH:mm:ss'Z'" // 2024-01-01T12:00:00Z 13"dd/MM/yyyy" // 01/01/2024 14"MM/dd/yyyy" // 01/01/2024 15"MMMM d, yyyy" // January 1, 2024 16"EEEE, MMMM d, yyyy" // Monday, January 1, 2024 17"h:mm a" // 12:00 PM
Format Conversion Quick Reference
Unix → ISO 8601
JAVASCRIPT1// JavaScript 2new Date(1704110400000).toISOString(); 3// 2024-01-01T12:00:00.000Z
PYTHON1# Python 2from datetime import datetime, timezone 3datetime.fromtimestamp(1704110400, timezone.utc).isoformat() 4# 2024-01-01T12:00:00+00:00
ISO 8601 → Unix
JAVASCRIPT1// JavaScript 2new Date('2024-01-01T12:00:00Z').getTime(); 3// 1704110400000
PYTHON1# Python 2from datetime import datetime 3datetime.fromisoformat('2024-01-01T12:00:00+00:00').timestamp() 4# 1704110400.0
RFC 2822 → ISO 8601
JAVASCRIPT1// JavaScript 2new Date('Mon, 01 Jan 2024 12:00:00 GMT').toISOString(); 3// 2024-01-01T12:00:00.000Z
PYTHON1# Python 2from email.utils import parsedate_to_datetime 3parsedate_to_datetime('Mon, 01 Jan 2024 12:00:00 GMT').isoformat() 4# 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