A practical, expert-led guide to convert time to epoch (Unix timestamp) accurately across languages, time zones, and formats—plus examples, pitfalls, and best practices.
Converting timestamps is a daily task for developers, analysts, and IT teams. If you often need to convert time to epoch (Unix timestamp), this guide will help you do it accurately and fast—across programming languages, spreadsheets, databases, and command line. You’ll also learn how to avoid common time zone and DST mistakes that break reports and logs.
To convert time to epoch (Unix timestamp), parse the local or UTC date-time, adjust to UTC, then output seconds (or milliseconds) since 1970-01-01 00:00:00 UTC. In code, use built-in date libraries (e.g., JavaScript Date, Python datetime). In Excel, convert to UTC and multiply days by 86,400. Or use a reliable online converter like ZenixTools to validate units and time zones.
This guide explains how to convert time to epoch (Unix timestamp) correctly, covering UTC vs local time, seconds vs milliseconds, and DST pitfalls. You’ll get language-specific examples (JavaScript, Python, Java, C#, PHP, Bash, SQL, Excel/Sheets), a manual formula, real-world use cases, and best practices. Includes a comparison table, expert tips, FAQs, and links to official references. Use ZenixTools to verify conversions and avoid unit/time zone errors.
“Convert time to epoch” means turning a human-readable date-time (like 2026-08-19 14:30:00) into a single integer that represents seconds (or milliseconds) since the Unix epoch: 1970-01-01 00:00:00 UTC.
Key points:
Related terms: Unix timestamp, POSIX time, Unix epoch, Unix time.
Follow these steps to convert any date-time to epoch correctly.
Seconds:
// Local time to epoch seconds (assuming local input)
const d = new Date('2026-08-19T14:30:00'); // if no offset, JS treats as local
const epochSeconds = Math.floor(d.getTime() / 1000);
// If input is UTC with 'Z' or offset
const dUtc = new Date('2026-08-19T14:30:00Z');
const epochSecondsUtc = Math.floor(dUtc.getTime() / 1000);
Milliseconds:
const epochMillis = Date.now(); // current time in ms
Tip: Always confirm whether your input has a trailing Z (UTC) or offset.
from datetime import datetime, timezone
# Aware UTC datetime
dt = datetime(2026, 8, 19, 14, 30, 0, tzinfo=timezone.utc)
epoch_seconds = int(dt.timestamp())
# Parsing local time with a known zone (pytz or zoneinfo)
from zoneinfo import ZoneInfo
local_dt = datetime(2026, 8, 19, 14, 30, 0, tzinfo=ZoneInfo('Europe/Berlin'))
epoch_sec_local = int(local_dt.timestamp()) # auto-converts to UTC under the hood
Parsing ISO 8601:
from datetime import datetime
iso = '2026-08-19T14:30:00+02:00'
dt = datetime.fromisoformat(iso)
epoch_seconds = int(dt.timestamp())
import java.time.*;
ZonedDateTime zdt = ZonedDateTime.of(2026, 8, 19, 14, 30, 0, 0, ZoneId.of("UTC"));
long epochSeconds = zdt.toEpochSecond();
// With offset
OffsetDateTime odt = OffsetDateTime.parse("2026-08-19T14:30:00+02:00");
long epochSeconds2 = odt.toEpochSecond();
// From local zone
ZonedDateTime local = ZonedDateTime.of(2026, 8, 19, 14, 30, 0, 0, ZoneId.of("Europe/Berlin"));
long epochSeconds3 = local.toEpochSecond();
using System;
var dt = new DateTime(2026, 8, 19, 14, 30, 0, DateTimeKind.Utc);
long epochSeconds = new DateTimeOffset(dt).ToUnixTimeSeconds();
var local = new DateTimeOffset(2026, 8, 19, 14, 30, 0, TimeSpan.FromHours(2));
long epochSeconds2 = local.ToUnixTimeSeconds();
// UTC example
$dt = new DateTime('2026-08-19 14:30:00', new DateTimeZone('UTC'));
$epoch = $dt->getTimestamp();
// With offset in string
$dt2 = new DateTime('2026-08-19T14:30:00+02:00');
$epoch2 = $dt2->getTimestamp();
# Local time to epoch seconds
date -d '2026-08-19 14:30:00' +%s
# Explicit UTC
date -u -d '2026-08-19 14:30:00' +%s
# macOS BSD date (note different flags)
TZ=UTC date -j -f '%Y-%m-%d %H:%M:%S' '2026-08-19 14:30:00' +%s
PostgreSQL:
-- To epoch seconds
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2026-08-19 14:30:00+02');
-- From local with time zone name
SET TIME ZONE 'Europe/Berlin';
SELECT EXTRACT(EPOCH FROM TIMESTAMP '2026-08-19 14:30:00');
MySQL / MariaDB:
-- Convert to UNIX timestamp (assumes value is in session time zone)
SELECT UNIX_TIMESTAMP('2026-08-19 14:30:00');
-- With explicit UTC
SET time_zone = '+00:00';
SELECT UNIX_TIMESTAMP('2026-08-19 14:30:00');
SQLite:
SELECT strftime('%s', '2026-08-19 14:30:00'); -- treated as UTC if 'Z' or offset is supplied
Formula (Excel):
=ROUND(((A2 - DATE(1970,1,1)) * 86400) - (TZ_OFFSET_SECONDS), 0)
Where A2 is a Date/Time value. For UTC inputs, TZ_OFFSET_SECONDS is 0. For e.g., UTC+2, use 7200.
Google Sheets (with ISO string):
=INT((VALUE(B2) - DATE(1970,1,1)) * 86400)
Tip: Ensure B2 is parsed as a valid Date/Time, not text. Use DATEVALUE/TIMEVALUE if needed.
| Method | Pros | Cons | Units | Best For |
|---|---|---|---|---|
| Built-in language libraries | Accurate, timezone-aware, fast | Requires correct TZ handling | s/ms | Apps, services, scripts |
| Database functions (SQL) | Close to data, fast queries | Session TZ pitfalls | s | Analytics, warehousing |
| Command line (date) | Quick, scriptable | Different flags across OS | s | DevOps, quick checks |
| Spreadsheets (Excel/Sheets) | Accessible to analysts | Parsing/TZ formulas tricky | s | Reporting, ad hoc calc |
| Online tools (ZenixTools) | Instant, transparent, cross-check | Manual step | s/ms | Validation, learning |
| Manual math | Teaches fundamentals | Error-prone, DST complexity | s | Rare, educational use |
What does “convert time to epoch” mean? Converting time to epoch means turning a date-time into the number of seconds (or milliseconds) since 1970-01-01 00:00:00 UTC, known as the Unix epoch.
How do I convert local time to epoch seconds? Parse the local time with its time zone, convert to UTC, then output seconds since 1970-01-01. Use built-in libraries (e.g., Python datetime, Java Time) or a tool like ZenixTools.
What’s the difference between epoch seconds and milliseconds? Seconds are 10 digits (e.g., 1692455400). Milliseconds are 13 digits (e.g., 1692455400000). Using the wrong unit causes 1000x errors.
Why is my timestamp off by a few hours? You likely misapplied time zones—treated local as UTC or double-applied the offset. Confirm the input’s zone or offset and convert to UTC first.
How do I handle DST during conversion? Use time zone-aware libraries (IANA zones like America/New_York). They know rules for nonexistent/ambiguous times. Avoid manual offsets near DST transitions.
Can I store epoch in a database? Yes. Use BIGINT for seconds or milliseconds. Consider indexes and partitions based on derived UTC dates for performance.
Should I store epoch or ISO 8601? Store epoch for performance and comparisons; expose ISO 8601 (with Z/offset) at interfaces for readability. Many teams store both.
How do I convert epoch back to a readable time? Add the epoch seconds to 1970-01-01 UTC using your language’s library, then format as ISO 8601 or convert to a display time zone.
What is the 2038 problem? On 32-bit systems, epoch seconds overflow after 2038-01-19. Modern 64-bit systems and languages avoid this. Use 64-bit integers.
Do leap seconds affect Unix time? Unix time typically ignores leap seconds, so it’s not a perfect measure of SI seconds. For most applications, this is acceptable.
How do I detect if a field is seconds or milliseconds? Check digit length (10 vs 13), or compare against a sanity range (e.g., current epoch seconds ≈ 1.7e9). Add validation in pipelines.
When you convert time to epoch, accuracy depends on three things: the correct time zone, the right unit (seconds vs milliseconds), and reliable tooling. Use timezone-aware libraries, test DST edge cases, and document units clearly. Validate conversions with ZenixTools to keep your logs, APIs, analytics, and schedules consistent and trustworthy across systems.
Try ZenixTools’ Time to Epoch Converter now. Paste a date-time, pick seconds or milliseconds, select your time zone, and get a verified Unix timestamp instantly—then cross-check with code samples from this guide to lock in accuracy.
Learn how to convert 1 meter to feet with precise formulas, quick methods, and real-world examples. Includes best practices, common mistakes, comparison tables, FAQs, and expert tips for accurate length conversions.
Learn how to convert 1 meter to feet with the exact formula, step-by-step instructions, quick mental math, and real-world examples. Includes charts, best practices, FAQs, and expert tips.
How can I convert time to epoch in Excel? Use (A2 − DATE(1970,1,1)) × 86,400, and adjust for time zone by subtracting the offset in seconds. Ensure A2 is a valid date-time value.
What time zone should I use for storage? Use UTC for storage. Convert to local time only for display.
Which languages have the easiest epoch conversions? Modern languages with timezone libraries (Python, Java, C#, JavaScript) make it straightforward. Use built-in methods like timestamp(), toEpochSecond(), ToUnixTimeSeconds().
Is there a reliable online converter? Yes. ZenixTools offers a fast, accurate Time to Epoch converter with clear unit and time zone controls to avoid mistakes.