Sun, Apr 19, 2026 14:02:27 UTC
Back to Blog

How to Convert Unix Timestamps in Every Programming Language (2026 Edition)

15 min read
codesnippetspythonjavascriptgorustjavareference
Code editor showing timestamp conversion in multiple programming languages

Every language handles timestamps slightly differently. Some count in seconds, others in milliseconds. Some have dedicated datetime types, others use raw integers. This guide gives you modern, idiomatic, copy-paste-ready code for the three operations every developer needs: getting the current epoch, converting epoch to date, and converting date to epoch.

All examples use the current recommended APIs for each language — no deprecated functions, no legacy patterns. If you're still using SimpleDateFormat in Java or time.gmtime() in Python, it's time to upgrade.

Get auto-filled code snippets for your active timestamp

JavaScript / TypeScript

JavaScript's Date object works in milliseconds, not seconds. The most common mistake is forgetting to multiply or divide by 1,000.

// Get current epoch (seconds)
const epoch = Math.floor(Date.now() / 1000);
// Epoch → Date
const date = new Date(1771387200 * 1000);
console.log(date.toISOString()); // "2026-02-18T12:00:00.000Z"
// Date → Epoch
const epoch2 = Math.floor(new Date('2026-02-18T12:00:00Z').getTime() / 1000);

Python

Python 3 has excellent timezone-aware datetime support. Always use timezone-aware objects with timezone.utc — naive datetimes are a common source of bugs.

from datetime import datetime, timezone
import time
# Get current epoch
epoch = int(time.time())
# Epoch → Date
dt = datetime.fromtimestamp(1771387200, tz=timezone.utc)
print(dt.isoformat()) # "2026-02-18T12:00:00+00:00"
# Date → Epoch
dt = datetime(2026, 2, 18, 12, 0, 0, tzinfo=timezone.utc)
epoch = int(dt.timestamp())

Go

Go's time package is one of the best-designed time APIs in any language. The unusual reference time "Mon Jan 2 15:04:05 MST 2006" is used for format strings instead of strftime-style codes.

package main
import (
"fmt"
"time"
)
func main() {
// Get current epoch
epoch := time.Now().Unix()
// Epoch → Date
t := time.Unix(1771387200, 0).UTC()
fmt.Println(t.Format(time.RFC3339)) // "2026-02-18T12:00:00Z"
// Date → Epoch
t2 := time.Date(2026, 2, 18, 12, 0, 0, 0, time.UTC)
fmt.Println(t2.Unix())
}

Rust

use std::time::{SystemTime, UNIX_EPOCH, Duration};
fn main() {
// Get current epoch
let epoch = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
// Epoch → SystemTime
let time = UNIX_EPOCH + Duration::from_secs(1771387200);
// For human-readable formatting, use the chrono crate:
// use chrono::{DateTime, Utc, NaiveDateTime};
// let dt = DateTime::<Utc>::from_timestamp(1771387200, 0).unwrap();
}

Java

Use java.time.Instant (Java 8+). Do not use Date, Calendar, or SimpleDateFormat — they are legacy APIs with timezone bugs and thread safety issues.

import java.time.Instant;
import java.time.ZoneOffset;
import java.time.LocalDateTime;
// Get current epoch
long epoch = Instant.now().getEpochSecond();
// Epoch → Date
Instant instant = Instant.ofEpochSecond(1771387200);
System.out.println(instant); // "2026-02-18T12:00:00Z"
// Date → Epoch
long epoch2 = LocalDateTime.of(2026, 2, 18, 12, 0, 0)
.toEpochSecond(ZoneOffset.UTC);

C#

// Get current epoch
long epoch = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
// Epoch → Date
DateTimeOffset dt = DateTimeOffset.FromUnixTimeSeconds(1771387200);
Console.WriteLine(dt.ToString("o")); // "2026-02-18T12:00:00.0000000+00:00"
// Date → Epoch
long epoch2 = new DateTimeOffset(2026, 2, 18, 12, 0, 0, TimeSpan.Zero)
.ToUnixTimeSeconds();

PHP

<?php
// Get current epoch
$epoch = time();
// Epoch → Date
$date = date('c', 1771387200); // "2026-02-18T12:00:00+00:00"
// Date → Epoch
$epoch = strtotime('2026-02-18T12:00:00Z');
// Or with DateTime:
$dt = new DateTime('2026-02-18T12:00:00Z');
$epoch = $dt->getTimestamp();

Bash

# Get current epoch
epoch=$(date +%s)
# Epoch → Date
date -d @1771387200 --iso-8601=seconds # Linux
date -r 1771387200 +%Y-%m-%dT%H:%M:%S # macOS
# Date → Epoch
date -d "2026-02-18T12:00:00Z" +%s # Linux
date -j -f "%Y-%m-%dT%H:%M:%S" "2026-02-18T12:00:00" +%s # macOS

SQL (MySQL, PostgreSQL, SQLite)

-- MySQL
SELECT UNIX_TIMESTAMP(); -- Current epoch
SELECT FROM_UNIXTIME(1771387200); -- Epoch → Date
SELECT UNIX_TIMESTAMP('2026-02-18 12:00:00'); -- Date → Epoch
-- PostgreSQL
SELECT extract(epoch FROM now()); -- Current epoch
SELECT to_timestamp(1771387200); -- Epoch → Date
SELECT extract(epoch FROM '2026-02-18 12:00:00'::timestamp); -- Date → Epoch
-- SQLite
SELECT strftime('%s', 'now'); -- Current epoch
SELECT datetime(1771387200, 'unixepoch'); -- Epoch → Date
SELECT strftime('%s', '2026-02-18 12:00:00'); -- Date → Epoch

For all 20+ supported languages with auto-filled values from your active conversion, use the code snippet generator:

Open the full code snippet generator