TimeStampConverter.org
Advertisement728×90 Leaderboard
JavaScript — Epoch Timestamps
Get current epoch (seconds)
Most common — matches Unix standard
// Seconds (10-digit) — standard Unix epoch
const epoch = Math.floor(Date.now() / 1000);
console.log(epoch); // e.g. 1717027200
Get epoch in milliseconds
JavaScript native — 13-digit
// Milliseconds (13-digit) — JS native
const epochMs = Date.now();
// or
const epochMs2 = new Date().getTime();
🔄
Epoch to Date object
Convert any Unix timestamp to a JS Date
const epoch = 1717027200;

// From seconds → multiply by 1000
const date = new Date(epoch * 1000);
console.log(date.toISOString());  // 2024-05-30T08:00:00.000Z
console.log(date.toLocaleString()); // Local time string
console.log(date.toUTCString());   // UTC string
📅
Date string to epoch
Parse any date string to a Unix timestamp
// Any parseable date string → epoch (seconds)
const toEpoch = (dateStr) =>
  Math.floor(new Date(dateStr).getTime() / 1000);

toEpoch('2024-05-30');            // 1717027200
toEpoch('2024-05-30T14:30:00Z');   // 1717079400
toEpoch('May 30 2024 09:00:00');   // local time
🌍
Format with timezone (Intl API)
Display a timestamp in any timezone
const epoch = 1717027200;
const date  = new Date(epoch * 1000);

const fmt = new Intl.DateTimeFormat('en-US', {
  timeZone: 'America/New_York',
  dateStyle: 'full',
  timeStyle: 'long',
});
console.log(fmt.format(date));
// Thursday, May 30, 2024 at 4:00:00 AM EDT
TypeScript — Epoch Timestamps
Typed epoch utilities
Type-safe timestamp helpers
// Branded types for safety
type EpochSeconds      = number & { readonly _brand: 'seconds' };
type EpochMilliseconds = number & { readonly _brand: 'ms' };

const nowSeconds = (): EpochSeconds =>
  Math.floor(Date.now() / 1000) as EpochSeconds;

const toDate = (epoch: EpochSeconds): Date =>
  new Date(epoch * 1000);

const fromDate = (d: Date): EpochSeconds =>
  Math.floor(d.getTime() / 1000) as EpochSeconds;
🔄
Parse & format utility class
Reusable timestamp class
class Timestamp {
  private readonly epoch: number;

  constructor(epoch: number) { this.epoch = epoch; }

  static now() { return new Timestamp(Math.floor(Date.now() / 1000)); }
  static from(d: Date) { return new Timestamp(Math.floor(d.getTime() / 1000)); }

  toDate(): Date   { return new Date(this.epoch * 1000); }
  toISO(): string { return this.toDate().toISOString(); }
  seconds(): number      { return this.epoch; }
  milliseconds(): number { return this.epoch * 1000; }
}
Python — Epoch Timestamps
Get current epoch
Using the time module
import time

# Seconds (float)
epoch_f = time.time()       # 1717027200.123
# Seconds (integer)
epoch_i = int(time.time())  # 1717027200
# Milliseconds
epoch_ms = int(time.time() * 1000)
🔄
Epoch ↔ datetime (stdlib)
Using datetime and timezone
from datetime import datetime, timezone

epoch = 1717027200

# Epoch → UTC datetime
dt_utc = datetime.fromtimestamp(epoch, tz=timezone.utc)
print(dt_utc.isoformat())  # 2024-05-30T08:00:00+00:00

# Epoch → local datetime
dt_local = datetime.fromtimestamp(epoch)

# datetime → epoch
back = int(dt_utc.timestamp())  # 1717027200
🌍
Timezone conversion with zoneinfo
Python 3.9+ built-in
from datetime import datetime, timezone
from zoneinfo import ZoneInfo

epoch = 1717027200
dt = datetime.fromtimestamp(epoch, tz=timezone.utc)

# Convert to any timezone
ny  = dt.astimezone(ZoneInfo("America/New_York"))
tok = dt.astimezone(ZoneInfo("Asia/Tokyo"))
print(ny.strftime("%Y-%m-%d %H:%M:%S %Z"))
📅
Parse date strings to epoch
Multiple formats
from datetime import datetime, timezone
import time

# ISO 8601 → epoch
dt = datetime.fromisoformat("2024-05-30T08:00:00+00:00")
epoch = int(dt.timestamp())  # 1717027200

# Custom format → epoch
dt2 = datetime.strptime("30/05/2024 08:00", "%d/%m/%Y %H:%M")
dt2 = dt2.replace(tzinfo=timezone.utc)
epoch2 = int(dt2.timestamp())
Go — Epoch Timestamps
Get current epoch
Using the time package
package main

import (
    "fmt"
    "time"
)

func main() {
    // Seconds
    epoch := time.Now().Unix()
    fmt.Println(epoch) // 1717027200

    // Milliseconds
    epochMs := time.Now().UnixMilli()

    // Nanoseconds
    epochNs := time.Now().UnixNano()
    _ = epochMs; _ = epochNs
}
🔄
Epoch ↔ time.Time
Convert in both directions
import "time"

// Epoch → time.Time (UTC)
epoch := int64(1717027200)
t := time.Unix(epoch, 0).UTC()
fmt.Println(t.Format(time.RFC3339)) // 2024-05-30T08:00:00Z

// time.Time → epoch
back := t.Unix() // 1717027200

// Parse string → epoch
parsed, _ := time.Parse(time.RFC3339, "2024-05-30T08:00:00Z")
fmt.Println(parsed.Unix())
🌍
Timezone conversion
Load any IANA timezone
loc, err := time.LoadLocation("America/New_York")
if err != nil {
    panic(err)
}

t := time.Unix(1717027200, 0).In(loc)
fmt.Println(t.Format("2006-01-02 15:04:05 MST"))
// 2024-05-30 04:00:00 EDT
Rust — Epoch Timestamps
Get current epoch (std only)
No external crates needed
use std::time::{SystemTime, UNIX_EPOCH};

fn main() {
    let epoch = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("Time went backwards");

    println!("Seconds: {}", epoch.as_secs());
    println!("Millis:  {}", epoch.as_millis());
    println!("Nanos:   {}", epoch.as_nanos());
}
🔄
With chrono crate
Add chrono = "0.4" to Cargo.toml
use chrono::{DateTime, TimeZone, Utc, Local};

// Current epoch
let now: DateTime<Utc> = Utc::now();
println!("{}", now.timestamp()); // seconds

// Epoch → DateTime
let dt = Utc.timestamp_opt(1717027200, 0).unwrap();
println!("{}", dt.to_rfc3339()); // 2024-05-30T08:00:00+00:00

// DateTime → epoch
let back = dt.timestamp();
Java — Epoch Timestamps
Get current epoch (Java 8+)
Using java.time (modern API)
import java.time.Instant;

// Seconds
long epoch = Instant.now().getEpochSecond();
// Milliseconds
long epochMs = Instant.now().toEpochMilli();
// or legacy
long epochMs2 = System.currentTimeMillis();
🔄
Epoch ↔ LocalDateTime
Convert between epoch and datetime
import java.time.*;

// Epoch → Instant → ZonedDateTime
Instant instant = Instant.ofEpochSecond(1717027200L);
ZonedDateTime utc = instant.atZone(ZoneOffset.UTC);
ZonedDateTime ny  = instant.atZone(ZoneId.of("America/New_York"));

// ZonedDateTime → epoch
long back = utc.toEpochSecond();

// Parse ISO string → epoch
ZonedDateTime parsed = ZonedDateTime.parse("2024-05-30T08:00:00Z");
long epoch = parsed.toEpochSecond();
C# — Epoch Timestamps
Get current epoch
Using DateTimeOffset
// Seconds
long epoch = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
// Milliseconds
long epochMs = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
🔄
Epoch ↔ DateTimeOffset
Full conversion in both directions
// Epoch → DateTimeOffset
var dto = DateTimeOffset.FromUnixTimeSeconds(1717027200);
Console.WriteLine(dto.ToString("o")); // ISO 8601

// Convert to a specific timezone
var tz = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var local = TimeZoneInfo.ConvertTime(dto, tz);

// DateTimeOffset → epoch
long back = dto.ToUnixTimeSeconds();
PHP — Epoch Timestamps
Get current epoch
// Seconds
$epoch = time();                 // 1717027200
// Milliseconds
$epochMs = round(microtime(true) * 1000);
🔄
Epoch ↔ DateTime
// Epoch → DateTime
$dt = new DateTime();
$dt->setTimestamp(1717027200);
echo $dt->format('Y-m-d H:i:s'); // 2024-05-30 08:00:00

// With timezone
$tz = new DateTimeZone('America/New_York');
$dt->setTimezone($tz);
echo $dt->format('Y-m-d H:i:s T');

// DateTime → epoch
$back = $dt->getTimestamp();

// Parse string → epoch
$epoch = strtotime('2024-05-30 08:00:00 UTC');
Ruby — Epoch Timestamps
Get current epoch
# Seconds (integer)
epoch = Time.now.to_i    # 1717027200
# Seconds (float)
epoch_f = Time.now.to_f  # 1717027200.123
# Milliseconds
epoch_ms = (Time.now.to_f * 1000).to_i
🔄
Epoch ↔ Time object
# Epoch → Time
t = Time.at(1717027200).utc
puts t.strftime("%Y-%m-%d %H:%M:%S %Z")
# 2024-05-30 08:00:00 UTC

# With timezone (requires tzinfo gem)
require 'tzinfo'
tz = TZInfo::Timezone.get('America/New_York')
local = tz.utc_to_local(t)

# Time → epoch
back = t.to_i
Bash / Shell — Epoch Timestamps
Get current epoch
# Linux / GNU date
date +%s

# macOS / BSD date
date -j +%s

# Portable (Python fallback)
python3 -c "import time; print(int(time.time()))"
🔄
Epoch → human date
# Linux
date -d @1717027200
date -d @1717027200 +"%Y-%m-%d %H:%M:%S"

# macOS
date -r 1717027200
date -r 1717027200 +"%Y-%m-%d %H:%M:%S"

# With timezone
TZ="America/New_York" date -d @1717027200
📅
Date string → epoch
# Linux
date -d "2024-05-30 08:00:00 UTC" +%s

# macOS
date -j -f "%Y-%m-%d %H:%M:%S" "2024-05-30 08:00:00" +%s

# Store in variable
EPOCH=$(date -d "2024-05-30" +%s)
echo $EPOCH
SQL — Epoch Timestamps
🐘
PostgreSQL
-- Current epoch (seconds)
SELECT EXTRACT(EPOCH FROM NOW())::BIGINT;

-- Epoch → timestamp
SELECT TO_TIMESTAMP(1717027200);
SELECT TO_TIMESTAMP(1717027200) AT TIME ZONE 'America/New_York';

-- Timestamp column → epoch
SELECT EXTRACT(EPOCH FROM created_at)::BIGINT FROM orders;

-- Date range using epochs
SELECT * FROM events
WHERE EXTRACT(EPOCH FROM created_at) BETWEEN 1717027200 AND 1717113600;
🐬
MySQL / MariaDB
-- Current epoch
SELECT UNIX_TIMESTAMP();

-- Epoch → datetime
SELECT FROM_UNIXTIME(1717027200);
SELECT FROM_UNIXTIME(1717027200, '%Y-%m-%d %H:%i:%s');

-- Datetime → epoch
SELECT UNIX_TIMESTAMP('2024-05-30 08:00:00');
SELECT UNIX_TIMESTAMP(created_at) FROM orders;
🪶
SQLite
-- Current epoch
SELECT strftime('%s', 'now');

-- Epoch → datetime
SELECT datetime(1717027200, 'unixepoch');
SELECT datetime(1717027200, 'unixepoch', 'localtime');

-- Datetime string → epoch
SELECT strftime('%s', '2024-05-30 08:00:00');
Swift — Epoch Timestamps
Get current epoch
import Foundation

// Seconds (Double)
let epoch = Date().timeIntervalSince1970
// Seconds (Int)
let epochInt = Int(Date().timeIntervalSince1970)
// Milliseconds
let epochMs = Int(Date().timeIntervalSince1970 * 1000)
🔄
Epoch ↔ Date + formatting
import Foundation

// Epoch → Date
let date = Date(timeIntervalSince1970: 1717027200)

// Format with DateFormatter
let fmt = DateFormatter()
fmt.dateStyle = .full
fmt.timeStyle = .medium
fmt.timeZone = TimeZone(identifier: "America/New_York")
print(fmt.string(from: date))

// Date → epoch
let back = Int(date.timeIntervalSince1970)
Kotlin — Epoch Timestamps
Get current epoch
import java.time.Instant

// Seconds
val epoch = Instant.now().epochSecond
// Milliseconds
val epochMs = Instant.now().toEpochMilli()
// or
val epochMs2 = System.currentTimeMillis()
🔄
Epoch ↔ ZonedDateTime
import java.time.*

// Epoch → ZonedDateTime
val instant = Instant.ofEpochSecond(1717027200L)
val utc   = instant.atZone(ZoneOffset.UTC)
val ny    = instant.atZone(ZoneId.of("America/New_York"))

println(utc.toString()) // 2024-05-30T08:00Z

// ZonedDateTime → epoch
val back = utc.toEpochSecond()

Unix Timestamp Code Examples

These code snippets cover the most common Unix timestamp operations in every major programming language: getting the current epoch, converting epoch to datetime, parsing date strings to epoch, and timezone-aware conversions. All snippets use standard library functions — no external dependencies required (except where noted).

Related tools

Copied!