Learn how to convert datetime to epoch (Unix timestamp) accurately across time zones and languages. Step-by-step guides, real examples, best practices, and a quick reference table for developers and analysts.
Converting datetime to epoch is a daily task for developers, data analysts, SREs, and anyone working with logs or APIs. Epoch (also called Unix time or POSIX time) lets you represent a moment as a single integer. That makes sorting, comparing, and storing time much simpler and faster.
Datetime to epoch means converting a human-readable date and time into a Unix timestamp: the number of seconds (or milliseconds) since 1970-01-01 00:00:00 UTC. To convert, parse the datetime, normalize it to UTC, then compute the elapsed seconds since the Unix epoch. Always confirm units: many systems use seconds; some use milliseconds or nanoseconds.
Epoch time represents a moment as the number of seconds (or milliseconds) since 1970-01-01 UTC. To convert datetime to epoch: parse the input, set or detect its time zone, normalize to UTC, then compute the difference from the epoch. Use built-in functions in your language (JS, Python, Java, SQL, Bash). Avoid mixing seconds and milliseconds, and always store times in UTC for consistency and speed.
“Datetime to epoch” is the process of converting a calendar date and time into a Unix timestamp. The Unix epoch starts at 1970-01-01 00:00:00 UTC. The timestamp is generally represented as:
Epoch time is also called Unix time or POSIX time. It’s widely used in operating systems, logs, databases, and APIs because it’s compact and easy to compare.
Note: Epoch time itself has no time zone. It is always anchored to UTC. Time zones only matter during parsing and display.
Real-world impact: API rate limiting, cache invalidation, log correlation, and scheduled jobs all rely on precise and consistent timing.
Follow these steps regardless of language or platform:
Tip: Prefer ISO 8601 (e.g., 2026-03-01T12:00:00-04:00) for unambiguous parsing.
// Seconds
const s = Math.floor(new Date('2026-08-25T12:34:56Z').getTime() / 1000);
// Milliseconds
const ms = new Date('2026-08-25T12:34:56-04:00').getTime();
// With Luxon
const { DateTime } = require('luxon');
const dt = DateTime.fromFormat('2026-08-25 12:34:56', 'yyyy-LL-dd HH:mm:ss', { zone: 'America/New_York' });
const seconds = Math.floor(dt.toMillis() / 1000);
Warning: new Date('YYYY-MM-DD HH:mm:ss') is ambiguous and may parse as UTC or local depending on environment. Favor ISO 8601 with Z or an explicit offset.
from datetime import datetime, timezone
# ISO 8601 with Z or offset
s = int(datetime.fromisoformat('2026-08-25T12:34:56+00:00').timestamp())
# If a naive local time that should be in America/New_York
from zoneinfo import ZoneInfo
naive = datetime.strptime('2026-08-25 12:34:56', '%Y-%m-%d %H:%M:%S')
aware = naive.replace(tzinfo=ZoneInfo('America/New_York'))
s_local = int(aware.timestamp())
# Milliseconds
ms = int(aware.timestamp() * 1000)
Note: datetime.timestamp() returns seconds (float). Multiply for milliseconds.
import java.time.*;
// ISO 8601 input
Instant instant = Instant.parse("2026-08-25T12:34:56Z");
long seconds = instant.getEpochSecond();
long millis = instant.toEpochMilli();
// Local time in a specific zone
ZonedDateTime zdt = LocalDateTime.parse("2026-08-25T12:34:56")
.atZone(ZoneId.of("America/New_York"));
long sLocal = zdt.toEpochSecond();
# Seconds from ISO 8601 (UTC)
date -u -d '2026-08-25T12:34:56Z' +%s
# Local time in a named zone
tz='America/New_York'; TZ=$tz date -d '2026-08-25 12:34:56' +%s
Note: BSD/macOS date syntax differs. On macOS, use: date -j -u -f '%Y-%m-%dT%H:%M:%SZ' '2026-08-25T12:34:56Z' '+%s'.
$dt = new DateTime('2026-08-25T12:34:56+00:00');
$seconds = $dt->getTimestamp();
$millis = $dt->getTimestamp() * 1000;
// Local time in zone
$dtLocal = new DateTime('2026-08-25 12:34:56', new DateTimeZone('America/New_York'));
$secondsLocal = $dtLocal->getTimestamp();
-- Seconds since epoch
SELECT EXTRACT(EPOCH FROM TIMESTAMP '2026-08-25 12:34:56+00');
-- Convert local time with zone
SELECT EXTRACT(EPOCH FROM (TIMESTAMP '2026-08-25 12:34:56' AT TIME ZONE 'America/New_York'));
-- Seconds since epoch (UTC)
SELECT UNIX_TIMESTAMP('2026-08-25 12:34:56');
-- If time has zone info, convert to UTC first (use CONVERT_TZ if needed)
SELECT strftime('%s', '2026-08-25 12:34:56'); -- assumes UTC or explicitly add 'Z'
var dt = DateTime.Parse("2026-08-25T12:34:56Z", null, System.Globalization.DateTimeStyles.RoundtripKind);
long seconds = new DateTimeOffset(dt).ToUnixTimeSeconds();
long millis = new DateTimeOffset(dt).ToUnixTimeMilliseconds();
// Local with zone (use NodaTime for robust TZ)
package main
import (
"fmt"
"time"
)
func main() {
t, _ := time.Parse(time.RFC3339, "2026-08-25T12:34:56Z")
fmt.Println(t.Unix()) // seconds
fmt.Println(t.UnixMilli()) // milliseconds (Go 1.17+)
}
use chrono::{DateTime, Utc, TimeZone};
let dt = DateTime::parse_from_rfc3339("2026-08-25T12:34:56Z").unwrap();
let seconds = dt.timestamp();
let millis = dt.timestamp_millis();
Warning: Never store milliseconds in a column or field expected to hold seconds. Document the unit in schema and API specs.
Example: You receive “2026-08-25 09:15:00 America/New_York” from a dashboard. Convert it to UTC, then to seconds since epoch to store in your job scheduler. At runtime, compare now_epoch >= start_epoch to trigger the task.
Below is a quick reference showing how to convert a datetime string to epoch across popular languages and tools.
| Environment | Convert Datetime to Epoch (UTC) | Unit |
|---|---|---|
| JavaScript | Math.floor(new Date('2026-08-25T12:34:56Z').getTime()/1000) | seconds |
| JavaScript | new Date('2026-08-25T12:34:56Z').getTime() | milliseconds |
| Python | int(datetime.fromisoformat('2026-08-25T12:34:56+00:00').timestamp()) | seconds |
| Java | Instant.parse("2026-08-25T12:34:56Z").getEpochSecond() | seconds |
| Java | Instant.parse("2026-08-25T12:34:56Z").toEpochMilli() | milliseconds |
| Bash (GNU date) | date -u -d '2026-08-25T12:34:56Z' +%s | seconds |
| PHP | (new DateTime('2026-08-25T12:34:56Z'))->getTimestamp() | seconds |
| PostgreSQL | EXTRACT(EPOCH FROM TIMESTAMP '2026-08-25 12:34:56+00') | seconds |
| MySQL |
Note: If your input is local time, first apply the correct IANA time zone, then convert to UTC before computing epoch.
Converting datetime to epoch is simple once you follow a consistent routine: parse the input, apply the correct time zone, normalize to UTC, and output the right unit. Store timestamps in UTC epoch, document units, and test near DST boundaries. With these practices, your systems will be faster, clearer, and less error-prone when handling time.
Work faster with time. Use ZenixTools to convert datetime to epoch, validate time zones, and back-convert for sanity checks. Try the Epoch Converter, Time Zone Converter, and ISO 8601 Formatter to standardize your workflows across apps and teams.
A complete, human-friendly guide to convert to WebP for faster sites and better SEO. Learn benefits, step-by-step workflows, code examples, and expert tips. Use ZenixTools to convert to WebP in seconds.
A practical, expert guide to convert base64 to string across languages, with steps, examples, pitfalls, and best practices.
UNIX_TIMESTAMP('2026-08-25 12:34:56')| seconds |
| SQLite | strftime('%s','2026-08-25 12:34:56Z') | seconds |
| C# | new DateTimeOffset(DateTime.Parse("2026-08-25T12:34:56Z")).ToUnixTimeSeconds() | seconds |
| Go | time.Parse(time.RFC3339, "2026-08-25T12:34:56Z").Unix() | seconds |
| Rust (chrono) | DateTime::parse_from_rfc3339("...").timestamp() | seconds |