Master everything about Unix timestamps from basic concepts to advanced applications
A Unix timestamp is a time representation method that indicates the number of seconds that have elapsed since January 1, 1970, 00:00:00 UTC (Coordinated Universal Time). This starting point is called the "Unix Epoch."
The choice of January 1, 1970, was not arbitrary. This date was a reasonable starting point during Unix system development - not too early to cause negative number issues, and not too late to affect historical data representation. This standard is now widely adopted as the universal time representation standard in computer systems.
1757304326
0
946684800
Unix timestamps can have different precision levels depending on application requirements. Understanding the use cases for various precision levels is crucial for choosing the appropriate timestamp format.
The most commonly used timestamp format, suitable for most application scenarios.
Current timestamp: 1757304326
Corresponding time: 2025-09-08T04:05:26.727Z
Commonly used format in JavaScript, providing higher time precision.
Current timestamp: 1757304326728
Corresponding time: 2025-09-08T04:05:26.728Z
Used for systems requiring extremely high time precision.
Current timestamp: 1757304326728000
Precision: Microseconds (1/1,000,000 second)
Different programming languages handle Unix timestamps in slightly different ways. Here are timestamp operation examples for mainstream programming languages:
// Get current timestamp (milliseconds)
const timestamp = Date.now();
console.log(timestamp); // 1703123456789
// Get current timestamp (seconds)
const timestampSeconds = Math.floor(Date.now() / 1000);
console.log(timestampSeconds); // 1703123456
// Convert timestamp to date
const date = new Date(timestamp);
console.log(date.toISOString()); // 2023-12-21T01:23:45.678Z
// Convert date to timestamp
const specificDate = new Date('2024-01-01T00:00:00Z');
const specificTimestamp = specificDate.getTime();
console.log(specificTimestamp); // 1704067200000
import time
import datetime
# Get current timestamp (seconds)
timestamp = time.time()
print(timestamp) # 1703123456.789
# Get current timestamp (integer seconds)
timestamp_int = int(time.time())
print(timestamp_int) # 1703123456
# Convert timestamp to date
dt = datetime.datetime.fromtimestamp(timestamp)
print(dt.isoformat()) # 2023-12-21T01:23:45.678901
# Convert date to timestamp
specific_date = datetime.datetime(2024, 1, 1, 0, 0, 0)
specific_timestamp = specific_date.timestamp()
print(specific_timestamp) # 1704067200.0
# UTC time handling
utc_dt = datetime.datetime.utcfromtimestamp(timestamp)
print(utc_dt.isoformat()) # 2023-12-21T01:23:45.678901
<?php
// Get current timestamp
$timestamp = time();
echo $timestamp; // 1703123456
// Convert timestamp to date
$date = date('Y-m-d H:i:s', $timestamp);
echo $date; // 2023-12-21 01:23:45
// Convert date to timestamp
$specificTimestamp = strtotime('2024-01-01 00:00:00');
echo $specificTimestamp; // 1704067200
// Using DateTime class (recommended)
$dateTime = new DateTime();
$timestamp = $dateTime->getTimestamp();
$dateTime = new DateTime('@' . $timestamp);
echo $dateTime->format('Y-m-d H:i:s'); // 2023-12-21 01:23:45
// Timezone handling
$dateTime = new DateTime('now', new DateTimeZone('UTC'));
$utcTimestamp = $dateTime->getTimestamp();
?>
import java.time.*;
import java.time.format.DateTimeFormatter;
// Get current timestamp (seconds)
long timestamp = Instant.now().getEpochSecond();
System.out.println(timestamp); // 1703123456
// Get current timestamp (milliseconds)
long timestampMillis = Instant.now().toEpochMilli();
System.out.println(timestampMillis); // 1703123456789
// Convert timestamp to date
Instant instant = Instant.ofEpochSecond(timestamp);
LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
System.out.println(dateTime); // 2023-12-21T01:23:45
// Convert date to timestamp
LocalDateTime specificDate = LocalDateTime.of(2024, 1, 1, 0, 0, 0);
long specificTimestamp = specificDate.toEpochSecond(ZoneOffset.UTC);
System.out.println(specificTimestamp); // 1704067200
// Format output
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDate = dateTime.format(formatter);
System.out.println(formattedDate); // 2023-12-21 01:23:45
In 32-bit systems, the maximum value for signed integers is 2,147,483,647, corresponding to January 19, 2038, 03:14:07 UTC. Exceeding this time will cause overflow.
Solutions:
Unix timestamps are inherently UTC time, but timezone conversion needs to be considered during display and input.
Best Practices:
Different application scenarios require different time precision levels. Choosing appropriate precision can optimize performance and storage.
Selection Guide:
Convert Unix epoch (seconds) to human-readable datetime in ISO 8601, RFC 3339, UTC and local formats.
Open ConverterUnix timestamps themselves are timezone-agnostic and always represent UTC. The timezone issue comes when you display or parse human-readable dates. Daylight Saving Time (DST) can shift local time by an hour, which frequently causes off-by-one-hour bugs in calendar features, reporting, and booking systems.
const ts = 1704067200000; // 2024-01-01T00:00:00.000Z (ms)
// Use Intl API to format in user's locale/timezone
const fmt = new Intl.DateTimeFormat(undefined, {
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
year: 'numeric', month: '2-digit', day: '2-digit',
hour: '2-digit', minute: '2-digit', second: '2-digit'
});
console.log(fmt.format(new Date(ts)));
from datetime import datetime, timezone
ts = 1704067200 # seconds
# UTC
utc_dt = datetime.fromtimestamp(ts, tz=timezone.utc)
print('UTC:', utc_dt.isoformat())
# Local
local_dt = datetime.fromtimestamp(ts)
print('Local:', local_dt.isoformat())
Never hardcode timezone offsets (like +0800). Use proper timezone databases (IANA tz, e.g., "America/New_York"). Offsets change with DST and political decisions.
Prefer ISO 8601 for interchange. When parsing in JavaScript, new Date('YYYY-MM-DD') may be treated as UTC in some engines and local in others—always include timezone or use Date.UTC.
// Avoid ambiguous parsing
const d1 = new Date('2024-01-01T00:00:00Z'); // explicit UTC
const d2 = new Date(Date.UTC(2024, 0, 1, 0, 0, 0)); // UTC via parts
// Format ISO
console.log(d1.toISOString()); // 2024-01-01T00:00:00.000Z
-- Store as BIGINT (ms) or TIMESTAMP WITH TIME ZONE
-- Convert BIGINT ms to timestamptz
SELECT to_timestamp(1704067200000 / 1000.0) AT TIME ZONE 'UTC';
-- Output ISO 8601
SELECT to_char(now() AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"');
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(now.Unix()) // seconds
fmt.Println(now.UnixMilli()) // milliseconds
// Parse ISO
t, _ := time.Parse(time.RFC3339, "2024-01-01T00:00:00Z")
fmt.Println(t.Unix())
}
using System;
class P {
static void Main() {
var now = DateTimeOffset.UtcNow;
Console.WriteLine(now.ToUnixTimeSeconds());
Console.WriteLine(now.ToUnixTimeMilliseconds());
var dt = DateTime.Parse("2024-01-01T00:00:00Z", null, System.Globalization.DateTimeStyles.AdjustToUniversal);
Console.WriteLine(new DateTimeOffset(dt).ToUnixTimeSeconds());
}
}
t = Time.now
puts t.to_i # seconds
puts (t.to_f * 1000).to_i # milliseconds
# Parse ISO
require 'time'
iso = Time.parse('2024-01-01T00:00:00Z')
puts iso.to_i
-- Convert milliseconds BIGINT to DATETIME
SELECT FROM_UNIXTIME(1704067200000 / 1000);
-- Current timestamp (ms)
SELECT UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000;
Likely a timezone mismatch. Your timestamp is UTC, but you’re displaying it as local without specifying timezone or the viewer is in a different timezone. Always store UTC, display in the user’s timezone.
Seconds are sufficient for most backend systems. Milliseconds are common in JavaScript and real-time UIs. Be consistent across services and document your precision.
Use UTC internally, convert to local only for display. Avoid scheduling logic in local time around DST boundaries; if necessary, use libraries and authoritative tz data.
Prefer ISO 8601 strings with explicit timezone (e.g., Z for UTC) and include a Unix timestamp field for machine processing. Provide both when possible.
This guide helps you comprehensively understand Unix timestamp concepts and applications. If you have any questions or suggestions, feel free to contact us.