Every programming language provides ways to work with Unix timestamps (epoch time). This comprehensive guide covers how to get, convert, and manipulate epoch time in the most popular programming languages. Use these code snippets alongside our epoch time converter to verify your results.
🟨 JavaScript / Node.js
JavaScript uses milliseconds for timestamps by default. The Date
object is the primary way to work with dates and times.
Get Current Epoch Time
// Current timestamp in milliseconds
const epochMs = Date.now();
console.log(epochMs); // e.g., 1734480000000
// Current timestamp in seconds
const epochSec = Math.floor(Date.now() / 1000);
console.log(epochSec); // e.g., 1734480000
// Using Date object
const now = new Date();
const epochFromDate = now.getTime(); // milliseconds
Convert Epoch to Date
// From seconds
const epochSeconds = 1734480000;
const date1 = new Date(epochSeconds * 1000);
console.log(date1.toISOString()); // "2024-12-17T00:00:00.000Z"
// From milliseconds
const epochMs = 1734480000000;
const date2 = new Date(epochMs);
console.log(date2.toLocaleString()); // Localized format
Convert Date to Epoch
// From date string
const dateStr = "2024-12-17T12:00:00Z";
const epoch = new Date(dateStr).getTime() / 1000;
console.log(epoch); // 1734436800
// From date components
const date = new Date(2024, 11, 17, 12, 0, 0); // Note: month is 0-indexed
const epochLocal = Math.floor(date.getTime() / 1000);
🐍 Python
Python's time and datetime modules provide comprehensive epoch time
support. Python uses seconds by default.
Get Current Epoch Time
import time
from datetime import datetime
# Current timestamp in seconds (float)
epoch_float = time.time()
print(epoch_float) # e.g., 1734480000.123456
# Current timestamp in seconds (integer)
epoch_int = int(time.time())
print(epoch_int) # e.g., 1734480000
# Using datetime
epoch_dt = datetime.now().timestamp()
print(epoch_dt)
Convert Epoch to Date
from datetime import datetime, timezone
epoch = 1734480000
# Local time
local_dt = datetime.fromtimestamp(epoch)
print(local_dt) # 2024-12-17 16:00:00 (depends on timezone)
# UTC time
utc_dt = datetime.fromtimestamp(epoch, tz=timezone.utc)
print(utc_dt) # 2024-12-17 00:00:00+00:00
# Formatted string
formatted = utc_dt.strftime('%Y-%m-%d %H:%M:%S')
print(formatted) # 2024-12-17 00:00:00
Convert Date to Epoch
from datetime import datetime, timezone
# From string
date_str = "2024-12-17 12:00:00"
dt = datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
epoch = int(dt.timestamp())
print(epoch)
# From components (UTC)
dt_utc = datetime(2024, 12, 17, 12, 0, 0, tzinfo=timezone.utc)
epoch_utc = int(dt_utc.timestamp())
print(epoch_utc) # 1734436800
☕ Java
Java uses milliseconds for timestamps. The modern java.time package
(Java 8+) is recommended.
Get Current Epoch Time
import java.time.Instant;
// Milliseconds
long epochMs = System.currentTimeMillis();
System.out.println(epochMs); // e.g., 1734480000000
// Seconds
long epochSec = System.currentTimeMillis() / 1000;
System.out.println(epochSec); // e.g., 1734480000
// Using Instant (Java 8+)
long epochInstant = Instant.now().getEpochSecond();
System.out.println(epochInstant);
Convert Epoch to Date
import java.time.*;
import java.time.format.DateTimeFormatter;
long epochSeconds = 1734480000L;
// To Instant
Instant instant = Instant.ofEpochSecond(epochSeconds);
System.out.println(instant); // 2024-12-17T00:00:00Z
// To LocalDateTime (local timezone)
LocalDateTime localDt = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
System.out.println(localDt);
// To ZonedDateTime
ZonedDateTime zonedDt = instant.atZone(ZoneId.of("America/New_York"));
System.out.println(zonedDt);
// Formatted
String formatted = zonedDt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(formatted);
Convert Date to Epoch
import java.time.*;
// From LocalDateTime
LocalDateTime dt = LocalDateTime.of(2024, 12, 17, 12, 0, 0);
long epoch = dt.atZone(ZoneId.of("UTC")).toEpochSecond();
System.out.println(epoch); // 1734436800
// From string
String dateStr = "2024-12-17T12:00:00";
LocalDateTime parsed = LocalDateTime.parse(dateStr);
long epochParsed = parsed.atZone(ZoneId.of("UTC")).toEpochSecond();
🐘 PHP
PHP uses seconds for timestamps. The time() function and
DateTime class are commonly used.
Get Current Epoch Time
<?php
// Current timestamp in seconds
$epoch = time();
echo $epoch; // e.g., 1734480000
// With milliseconds
$epochMs = round(microtime(true) * 1000);
echo $epochMs; // e.g., 1734480000000
// Using DateTime
$dt = new DateTime();
$epoch = $dt->getTimestamp();
echo $epoch;
Convert Epoch to Date
<?php
$epoch = 1734480000;
// Using date()
$formatted = date('Y-m-d H:i:s', $epoch);
echo $formatted; // 2024-12-17 00:00:00
// Using DateTime
$dt = new DateTime("@$epoch");
$dt->setTimezone(new DateTimeZone('America/New_York'));
echo $dt->format('Y-m-d H:i:s T'); // With timezone
Convert Date to Epoch
<?php
// Using strtotime
$epoch = strtotime('2024-12-17 12:00:00');
echo $epoch; // 1734436800
// Using DateTime
$dt = new DateTime('2024-12-17 12:00:00', new DateTimeZone('UTC'));
$epoch = $dt->getTimestamp();
echo $epoch;
🔷 Go
Go's time package provides excellent timestamp support. Go uses
seconds for Unix() and nanoseconds for UnixNano().
Get Current Epoch Time
package main
import (
"fmt"
"time"
)
func main() {
// Current timestamp in seconds
epochSec := time.Now().Unix()
fmt.Println(epochSec) // e.g., 1734480000
// Milliseconds
epochMs := time.Now().UnixMilli()
fmt.Println(epochMs) // e.g., 1734480000000
// Nanoseconds
epochNano := time.Now().UnixNano()
fmt.Println(epochNano)
}
Convert Epoch to Date
package main
import (
"fmt"
"time"
)
func main() {
epochSec := int64(1734480000)
// From seconds
t := time.Unix(epochSec, 0)
fmt.Println(t) // 2024-12-17 00:00:00 +0000 UTC
// Formatted
formatted := t.Format("2006-01-02 15:04:05")
fmt.Println(formatted) // 2024-12-17 00:00:00
// In specific timezone
loc, _ := time.LoadLocation("America/New_York")
localTime := t.In(loc)
fmt.Println(localTime)
}
Convert Date to Epoch
package main
import (
"fmt"
"time"
)
func main() {
// From components
t := time.Date(2024, 12, 17, 12, 0, 0, 0, time.UTC)
epoch := t.Unix()
fmt.Println(epoch) // 1734436800
// From string
layout := "2006-01-02 15:04:05"
parsed, _ := time.Parse(layout, "2024-12-17 12:00:00")
epochParsed := parsed.Unix()
fmt.Println(epochParsed)
}
💎 Ruby
Ruby's Time class handles epoch time with ease. Ruby uses seconds
by default.
Get Current Epoch Time
# Current timestamp in seconds
epoch = Time.now.to_i
puts epoch # e.g., 1734480000
# With fractional seconds
epoch_float = Time.now.to_f
puts epoch_float # e.g., 1734480000.123456
# Milliseconds
epoch_ms = (Time.now.to_f * 1000).to_i
puts epoch_ms
Convert Epoch to Date
epoch = 1734480000
# To Time object
t = Time.at(epoch)
puts t # 2024-12-17 00:00:00 UTC
# UTC time
t_utc = Time.at(epoch).utc
puts t_utc
# Formatted
formatted = t.strftime('%Y-%m-%d %H:%M:%S')
puts formatted # 2024-12-17 00:00:00
Convert Date to Epoch
require 'time'
# From string
t = Time.parse('2024-12-17 12:00:00 UTC')
epoch = t.to_i
puts epoch # 1734436800
# From components
t = Time.utc(2024, 12, 17, 12, 0, 0)
epoch = t.to_i
puts epoch
💜 C# / .NET
C# uses DateTimeOffset for Unix timestamp operations. .NET uses
seconds for Unix time.
Get Current Epoch Time
using System;
// Current timestamp in seconds
long epochSec = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
Console.WriteLine(epochSec); // e.g., 1734480000
// Milliseconds
long epochMs = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
Console.WriteLine(epochMs); // e.g., 1734480000000
Convert Epoch to Date
using System;
long epoch = 1734480000;
// To DateTimeOffset
DateTimeOffset dto = DateTimeOffset.FromUnixTimeSeconds(epoch);
Console.WriteLine(dto); // 12/17/2024 12:00:00 AM +00:00
// To local time
DateTime localTime = dto.LocalDateTime;
Console.WriteLine(localTime);
// Formatted
string formatted = dto.ToString("yyyy-MM-dd HH:mm:ss");
Console.WriteLine(formatted); // 2024-12-17 00:00:00
Convert Date to Epoch
using System;
// From DateTime
DateTime dt = new DateTime(2024, 12, 17, 12, 0, 0, DateTimeKind.Utc);
DateTimeOffset dto = new DateTimeOffset(dt);
long epoch = dto.ToUnixTimeSeconds();
Console.WriteLine(epoch); // 1734436800
// From current time
long nowEpoch = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
🖥️ Bash / Shell
Unix shells use the date command for epoch time operations. Uses
seconds by default.
Get Current Epoch Time
# Current timestamp in seconds
date +%s
# Output: 1734480000
# With nanoseconds
date +%s%N
# Output: 1734480000123456789
# Milliseconds
echo $(($(date +%s%N)/1000000))
Convert Epoch to Date
# From epoch to date (GNU date)
date -d @1734480000
# Output: Tue Dec 17 00:00:00 UTC 2024
# Formatted
date -d @1734480000 '+%Y-%m-%d %H:%M:%S'
# Output: 2024-12-17 00:00:00
# macOS/BSD
date -r 1734480000 '+%Y-%m-%d %H:%M:%S'
Convert Date to Epoch
# Date string to epoch (GNU date)
date -d '2024-12-17 12:00:00 UTC' +%s
# Output: 1734436800
# macOS/BSD
date -j -f '%Y-%m-%d %H:%M:%S' '2024-12-17 12:00:00' +%s
🔄 Verify Your Conversions
Use our free epoch time converter to verify your timestamp conversions and experiment with different formats.
Open Epoch Time Converter