A complete, step-by-step guide for developers on converting Unix timestamps (Epoch time) into human-readable dates.
Result will appear here...
🔥 Popular: 1,200+ developers used this tool in the last 24 hours
A Unix timestamp is the total number of seconds that have passed since January 1, 1970, 00:00:00 UTC. This moment is known as the Unix Epoch. Because it's a simple integer, it's a universal standard for time in computing, free from timezone complications. To "convert" a timestamp means to calculate the exact date and time it represents.
Before writing any code, you must know if your timestamp is in seconds or milliseconds. This is the #1 source of errors.
1704067200 — Used by PHP, Python, Ruby, and most databases.1704067200000 — Used by JavaScript and Java.If you use a seconds-based timestamp in a function that expects milliseconds (like `new Date()` in JavaScript), you will get a date in early 1970. Always multiply a seconds timestamp by 1000 to get milliseconds.
Here’s how to convert a timestamp to a date in various programming languages. All examples assume a starting timestamp of `1704067200` (which is 2024-01-01 00:00:00 UTC).
JavaScript's `Date` object works with milliseconds. You must multiply a 10-digit timestamp by 1000.
const timestamp = 1704067200;
// 1. Multiply by 1000 to convert seconds to milliseconds
const date = new Date(timestamp * 1000);
// 2. Format it to a standard string (ISO 8601)
console.log(date.toISOString());
// Output: "2024-01-01T00:00:00.000Z"
// 3. Format it to a locale-specific string
console.log(date.toLocaleString('en-US', { timeZone: 'America/New_York' }));
// Output: "12/31/2023, 7:00:00 PM"Python's `datetime` library makes this straightforward. The `fromtimestamp()` function handles it directly.
import datetime
timestamp = 1704067200
# Convert to a naive datetime object (local timezone)
date_local = datetime.datetime.fromtimestamp(timestamp)
# Best practice: convert to a timezone-aware object in UTC
date_utc = datetime.datetime.fromtimestamp(timestamp, tz=datetime.timezone.utc)
print(date_utc.strftime('%Y-%m-%d %H:%M:%S %Z'))
# Output: "2024-01-01 00:00:00 UTC"PHP's `date()` function is designed to work with Unix timestamps in seconds.
<?php
$timestamp = 1704067200;
// Format the timestamp directly
$date_string = date('Y-m-d H:i:s', $timestamp);
echo $date_string;
// Output: "2024-01-01 00:00:00"
// For timezone-aware conversion, use DateTime object
$date = new DateTime('@' . $timestamp);
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format('Y-m-d H:i:s');
// Output: "2023-12-31 19:00:00"
?>Java's modern `java.time` package uses the `Instant` class, which is perfect for this.
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
class Main {
public static void main(String[] args) {
long timestamp = 1704067200L; // Use long for timestamps
// Create an Instant from seconds
Instant instant = Instant.ofEpochSecond(timestamp);
// Format it in UTC
System.out.println(instant.toString());
// Output: "2024-01-01T00:00:00Z"
// Format it in a specific timezone
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
.withZone(ZoneId.of("America/New_York"));
System.out.println(formatter.format(instant));
// Output: "2023-12-31 19:00:00"
}
}C# uses the `DateTimeOffset` structure to handle this conversion correctly.
using System;
public class Converter
{
public static void Main(string[] args)
{
long timestamp = 1704067200;
// Convert from seconds
DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(timestamp);
// Get the DateTime object in UTC
DateTime utcDateTime = dateTimeOffset.UtcDateTime;
Console.WriteLine(utcDateTime.ToString("o"));
// Output: "2024-01-01T00:00:00.0000000Z"
}
}Most SQL databases have built-in functions to convert timestamps.
-- PostgreSQL
SELECT to_timestamp(1704067200);
-- Result: 2024-01-01 00:00:00+00
-- MySQL
SELECT FROM_UNIXTIME(1704067200);
-- Result: 2024-01-01 00:00:00
-- SQL Server
SELECT DATEADD(s, 1704067200, '1970-01-01');
-- Result: 2024-01-01 00:00:00.000This is the most common issue and it's almost always caused by a mismatch in precision. You are likely using a 10-digit timestamp (seconds) in a function that expects a 13-digit timestamp (milliseconds), like JavaScript's `new Date()`. To fix this, multiply your seconds timestamp by 1000 before passing it to the function.
In most languages, you can get the timestamp by calling a method on a date object. For example, in JavaScript, you can use `Math.floor(myDate.getTime() / 1000)` to get the timestamp in seconds. In Python, it's `int(my_date.timestamp())`.
No, a Unix timestamp is always based on UTC (Coordinated Universal Time). It is a universal, timezone-independent value. Timezone only becomes a factor when you want to display the timestamp as a human-readable date in a specific local time (e.g., 'America/New_York'). The conversion from the timestamp to the date components (year, month, day) is the same everywhere.