timestamper.online

How to Convert Unix Timestamp to Date

A complete, step-by-step guide for developers on converting Unix timestamps (Epoch time) into human-readable dates.

🚀 Instant Conversion
Don't have time for code? Paste your timestamp here for an instant result.
🚀 Try It Now - Convert Instantly
Paste your Unix timestamp below to see instant conversion

Result will appear here...

🔥 Popular: 1,200+ developers used this tool in the last 24 hours

Understanding the Conversion

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.

Step 1: Check Your Timestamp's Precision

Before writing any code, you must know if your timestamp is in seconds or milliseconds. This is the #1 source of errors.

  • Seconds (10 digits): 1704067200 — Used by PHP, Python, Ruby, and most databases.
  • Milliseconds (13 digits): 1704067200000 — Used by JavaScript and Java.

Crucial Warning

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.

Conversion in Code: Examples

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 (Node.js & Browser)

JavaScript's `Date` object works with milliseconds. You must multiply a 10-digit timestamp by 1000.

javascript
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

Python's `datetime` library makes this straightforward. The `fromtimestamp()` function handles it directly.

python
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

PHP's `date()` function is designed to work with Unix timestamps in seconds.

php
<?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

Java's modern `java.time` package uses the `Instant` class, which is perfect for this.

java
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#

C# uses the `DateTimeOffset` structure to handle this conversion correctly.

csharp
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"
    }
}

In SQL Databases

Most SQL databases have built-in functions to convert timestamps.

sql
-- 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.000

Frequently Asked Questions

Why is my converted date showing the year 1970?

This 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.

How do I convert a date back to a Unix timestamp?

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())`.

Does timezone matter when converting a Unix 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.

Continue Your Learning
Dive deeper into related topics with our other guides.