Learn how to convert a date to epoch (Unix time) in seconds or milliseconds with step-by-step instructions, code examples, tips, and a free ZenixTools converter.
Converting a date to epoch is a common task in development, data work, and logging. Epoch (also called Unix time) counts seconds or milliseconds since 1970-01-01 00:00:00 UTC. In this guide, you will learn what epoch is, why it matters, and how to convert any date to epoch with ZenixTools and popular languages.
Quick, correct conversion saves time and prevents bugs, especially around time zones and daylight saving time.
Featured Snippet Answer: To convert a date to epoch (Unix time), parse the date in UTC, then compute the number of seconds since 1970-01-01 00:00:00 UTC. In JavaScript, use Date.UTC or a Date object and divide milliseconds by 1000. In Python, use datetime with timezone.utc and call timestamp. Always confirm seconds vs milliseconds and set the correct time zone.
Convert a date to epoch by parsing the date in UTC, then measuring elapsed time since 1970-01-01 00:00:00 UTC. Use ZenixTools to convert instantly or code in Python, JavaScript, Java, Bash, SQL, or spreadsheets. Watch for time zone, daylight saving, and seconds vs milliseconds. Best practice: use ISO 8601, UTC, and consistent units. This guide includes steps, examples, pitfalls, and a comparison of methods.
Date to epoch means converting a human-readable date and time into a Unix timestamp. Unix time counts the elapsed time since the Unix epoch: 1970-01-01 00:00:00 UTC.
Key points:
Common formats you will see:
Related terms:
Time is a foundation for logs, metrics, and APIs. When systems agree on time, data lines up. Epoch time is compact and unambiguous. It avoids confusion across languages and regions.
Practical reasons it matters:
If you want a quick, error-free conversion, use the ZenixTools converter.
Steps:
Notes:
from datetime import datetime, timezone
# ISO 8601 string in UTC
ds = '2024-12-31T23:59:59Z'
dt = datetime.fromisoformat(ds.replace('Z', '+00:00'))
# Seconds since epoch
sec = int(dt.timestamp())
# Milliseconds since epoch
ms = int(dt.timestamp() * 1000)
print(sec, ms)
Local time with zone:
from datetime import datetime
import zoneinfo # Python 3.9+
ny = zoneinfo.ZoneInfo('America/New_York')
dt_local = datetime(2024, 3, 10, 2, 30, 0, tzinfo=ny) # DST tricky hour
sec = int(dt_local.timestamp())
Tip: Prefer timezone-aware datetimes and zoneinfo for accuracy.
// ISO 8601 in UTC
const s = '2024-12-31T23:59:59Z';
const ms = new Date(s).getTime(); // milliseconds
const sec = Math.floor(ms / 1000);
// Constructing UTC explicitly
const msUtc = Date.UTC(2024, 11, 31, 23, 59, 59); // month is 0-based
const secUtc = Math.floor(msUtc / 1000);
console.log(sec, secUtc);
Avoid parsing ambiguous formats like 12/31/2024. Use ISO 8601.
import java.time.*;
public class EpochExample {
public static void main(String[] args) {
Instant instant = Instant.parse("2024-12-31T23:59:59Z");
long sec = instant.getEpochSecond();
long ms = instant.toEpochMilli();
ZonedDateTime zdt = ZonedDateTime.of(2024, 3, 10, 2, 30, 0, 0, ZoneId.of("America/New_York"));
long secLocal = zdt.toEpochSecond();
System.out.println(sec + " " + ms + " " + secLocal);
}
}
Use java.time, not legacy Date or Calendar.
Current time in seconds:
date +%s
From a specific date in UTC:
# GNU date
date -u -d '2024-12-31 23:59:59' +%s
Be aware that macOS uses BSD date. Use -j -f for parsing on macOS.
PostgreSQL:
-- seconds since epoch from UTC timestamp
SELECT EXTRACT(EPOCH FROM TIMESTAMP '2024-12-31 23:59:59+00');
-- milliseconds
SELECT (EXTRACT(EPOCH FROM TIMESTAMP '2024-12-31 23:59:59+00') * 1000)::bigint;
MySQL:
-- seconds since epoch
SELECT UNIX_TIMESTAMP('2024-12-31 23:59:59');
-- milliseconds (MySQL 5.6+ with microseconds)
SELECT UNIX_TIMESTAMP('2024-12-31 23:59:59.000') * 1000;
SQLite:
-- seconds
SELECT strftime('%s', '2024-12-31 23:59:59');
Excel formula (seconds):
=INT((A1 - DATE(1970,1,1)) * 86400)
Excel with time zone offset (hours in B1):
=INT(((A1 - (B1/24)) - DATE(1970,1,1)) * 86400)
Google Sheets (seconds):
=INT((A1 - DATE(1970,1,1)) * 86400)
For milliseconds, multiply by 1000. Ensure A1 is in UTC or adjust with offsets.
This helps explain how libraries compute epoch values.
You receive logs from five services in different zones. Convert all timestamps to epoch seconds. Sort by epoch to rebuild the timeline. Use ZenixTools or language libraries for consistent results.
A token includes an exp claim measured in seconds since epoch. Your service compares current epoch time to exp. If now is greater, reject the token. Using UTC and seconds ensures consistent behavior.
A table stores created_at_epoch_ms as BIGINT. To fetch last 7 days:
SELECT *
FROM orders
WHERE created_at_epoch_ms >= (EXTRACT(EPOCH FROM NOW()) * 1000) - 7*24*60*60*1000;
Numeric comparison is fast and index-friendly.
A web app records session start in milliseconds. It computes session length by subtracting start from end without parsing strings. This reduces CPU and avoids locale issues.
A system accepts a run_at epoch timestamp. Convert your local time to epoch in UTC, send it, and the scheduler runs the job at the right instant regardless of daylight saving time.
Warning: When precision matters, confirm whether the API expects seconds or milliseconds.
| Method | Pros | Cons | Best for |
|---|---|---|---|
| ZenixTools Converter | Fast, no code, handles zones, copy-ready | Manual step, needs internet | One-off conversions, QA, support |
| Python datetime | Precise, rich tz support | Requires code, version care | Data pipelines, ETL |
| JavaScript Date | Ubiquitous, simple | Ambiguous parsing, local vs UTC traps | Frontend, Node scripts |
| Java java.time | Strong typing, zone-aware | More verbose | Backends, Android, enterprise |
| Bash date | Quick CLI | GNU vs BSD differences | DevOps, shell scripts |
| SQL functions | Runs in DB, fast filters | Dialect differences | Analytics, large queries |
| Excel/Sheets | Easy for analysts | Time zone handling is manual | Ad-hoc analysis, CSV data |
Epoch time, or Unix time, is the number of seconds or milliseconds since 1970-01-01 00:00:00 UTC. It is a standard numeric timestamp used across systems.
Both exist. Classic Unix time uses seconds. Many modern systems and browsers use milliseconds. Always check your target system and keep the unit consistent.
Create a Date from an ISO 8601 string and call getTime for milliseconds. Divide by 1000 and floor for seconds. Example: Math.floor(new Date('2024-12-31T23:59:59Z').getTime() / 1000).
Parse the date into a timezone-aware datetime in UTC and call timestamp. Example: int(datetime.fromisoformat('2024-12-31T23:59:59+00:00').timestamp()).
Because of time zone defaults, locale parsing, and differences in GNU vs BSD utilities. Always specify UTC, use ISO 8601, and pin your tools.
Practically, for computing, they refer to the same zero-offset zone. UTC is the modern standard. Use UTC for epoch conversions.
Classic Unix time ignores leap seconds. Most systems smear or ignore them, so you may see a one-second difference during leap events.
Check the length and range. A 10-digit number today is likely seconds. A 13-digit number is likely milliseconds. Or compare against current time.
32-bit signed seconds overflow around 2038-01-19. Modern 64-bit systems and libraries avoid this. Audit legacy code that uses 32-bit time types.
Convert to UTC first. If you must accept local times, include the zone or offset. Use official IANA time zone data for accuracy.
Yes. Use ZenixTools Epoch to Date or call new Date(ms) in JavaScript, datetime.fromtimestamp in Python, or to_timestamp in SQL dialects.
Browsers, JavaScript, and many modern systems measure time in milliseconds. It offers greater precision. Always read the API docs to confirm.
Yes. Use ISO 8601 or RFC 3339. Example: 2024-12-31T23:59:59Z for UTC. It is unambiguous and widely supported.
Use integer types. BIGINT for milliseconds. Index the column for range queries. Document the unit in schema comments.
Convert epoch to a time object in UTC, then format using a local zone. In JavaScript, use toLocaleString with timeZone. In Python, use astimezone.
Epoch timestamps make time portable, precise, and fast for machines. When you convert a date to epoch, always set UTC, pick the right unit, and use reliable libraries. This guide covered steps, code, and pitfalls so your results are accurate across systems. For the fastest path, use the ZenixTools Date to Epoch Converter and stop guessing when you need to convert a date to epoch.
Convert dates to epoch in seconds or milliseconds now. Open the free ZenixTools Date to Epoch Converter, set your time zone, and copy a clean, accurate timestamp in one click.
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.
Master converting from kilometers to miles with exact formulas, quick mental math, charts, and real examples. Written for travelers, runners, students, and pros.