Quick Summary
A cloud startup launched in 2024 with a clean SaaS bill […]
What Is Epoch Time? • The One-Number Clock • Why Epoch Time Matters for Duration Calculations
A cloud startup launched in 2024 with a clean SaaS billing engine — or so they thought. Users were charged by the minute for compute time. Six months later, customer support was drowning in complaints: bills were 60 times too high. The root cause? The engineering team had subtracted two epoch timestamps and fed the raw result straight into the billing formula — without dividing by 60. Seconds were billed as minutes.
That single missing division operator cost the company $340,000 in refunds and a wave of trust erosion it never fully recovered from.
The math itself is trivially simple. The devil is in the unit conversion. Let’s walk through it properly.
What Is Epoch Time?
The One-Number Clock
Epoch time (also called Unix time or POSIX time) counts the number of seconds elapsed since 00:00:00 UTC on January 1, 1970 — leap seconds excluded. It is stored as a single integer, and it is used by virtually every programming language, database, and operating system on the planet.
| Example Epoch Value | Human-Readable Date |
|---|---|
1609459200 |
January 1, 2021 00:00:00 UTC |
1609462800 |
January 1, 2021 01:00:00 UTC |
1680000000 |
March 28, 2023 16:00:00 UTC |
Subtract 1609459200 from 1609462800 and you get 3600. That is 3600 seconds — exactly one hour.
Why Epoch Time Matters for Duration Calculations
| Advantage | Explanation |
|---|---|
| Simplicity | A single integer — no date parsing needed |
| Universality | Every platform speaks it natively |
| Precision | Sub-second resolution (milliseconds, nanoseconds) when needed |
| Timezone-free | Always UTC — no ambiguity |
But here is the critical detail many developers overlook: epoch values are always in seconds (or milliseconds, or nanoseconds) — never in minutes. If you want minutes, you must convert.
The Math: Epoch Difference to Minutes
Step-by-Step Formula
Given two epoch timestamps:
start_time = 1680000000
end_time = 1680001800
Step 1 — Subtract to get the difference in seconds:
difference_seconds = end_time - start_time
Step 2 — Divide by 60 to get minutes:
difference_minutes = difference_seconds / 60
# Result: 30 minutes
That is the entire formula: (end – start) / 60.
The Unit Conversion Table
Before doing any math, confirm which unit your timestamps use. A millisecond timestamp looks deceptively like a second timestamp — just 1000 times larger.
| Input Unit | Convert to Seconds | Then to Minutes |
|---|---|---|
| Seconds | No conversion needed | / 60 |
| Milliseconds | / 1000 |
/ 1000 / 60 |
| Microseconds | / 1,000,000 |
/ 1,000,000 / 60 |
| Nanoseconds | / 1,000,000,000 |
/ 1,000,000,000 / 60 |
Code Snippets
Python:
minutes = (end_epoch - start_epoch) / 60
JavaScript:
let minutes = (endEpoch - startEpoch) / 60;
Bash:
minutes=$(( (end_epoch - start_epoch) / 60 ))
Go:
minutes := (endEpoch - startEpoch) / 60
Real-World Applications
Server Session Duration
A server logs a user login at epoch 1700000000 and logout at epoch 1700003600. To determine the session length:
duration_minutes = (1700003600 - 1700000000) / 60
= 3600 / 60
= 60 minutes
System administrators use this pattern to track usage patterns, detect anomalies, and enforce session timeouts.
SaaS Billing by the Minute
Cloud platforms — from AWS Lambda to Vercel — bill compute time per minute. Accurate epoch-to-minute conversion is not optional; it is a revenue-critical calculation.
The startup story from the opening is not hypothetical. Unit conversion errors in billing systems are a well-documented class of bug, and they are almost always caused by treating seconds as minutes or forgetting a division step.
Event Scheduling and Reminders
Calendar applications store event times as epoch values internally. When calculating “notify me 15 minutes before,” the system computes:
reminder_epoch = event_epoch - (15 * 60)
The multiplication by 60 converts minutes back into seconds — the inverse of the subtraction workflow.
Common Pitfalls
Mistaking Milliseconds for Seconds
Some APIs return milliseconds, not seconds. JavaScript’s Date.now() is a classic trap:
Date.now() // returns milliseconds — e.g., 1700000000000
Math.floor(Date.now() / 1000) // converts to seconds correctly
| Language / API | Default Unit |
|---|---|
Python time.time() |
Seconds |
JavaScript Date.now() |
Milliseconds |
Java System.currentTimeMillis() |
Milliseconds |
Go time.Now().Unix() |
Seconds |
Go time.Now().UnixMilli() |
Milliseconds |
Always check the documentation before performing arithmetic.
Ignoring Time Zones During Display
Epoch values are timezone-neutral — they represent UTC. But when you convert an epoch to a human-readable string for display, you must specify a time zone. Libraries like pytz or zoneinfo (Python), Luxon or date-fns (JavaScript), and Intl (Java) handle this correctly.
Negative Results
If end_time is earlier than start_time, the result is negative. This is not an error — it simply means the events are in reverse chronological order. Useful for countdown timers or anomaly detection.
Best Practices
- Always confirm the unit — seconds, milliseconds, or nanoseconds — before doing any math.
- Use built-in libraries when available (
datetimein Python,Datein JavaScript,Instant+Durationin Java). They handle edge cases like daylight saving transitions. - Wrap the conversion in a helper function so the division-by-60 logic is centralized and testable.
- Add unit tests that verify the conversion with known epoch pairs.
FAQ
What is the easiest way to get the difference between two epoch timestamps in minutes?
Divide the raw difference by 60:
minutes = (end_epoch - start_epoch) / 60
Are all epoch timestamps measured in seconds?
No. Some systems use milliseconds (multiply by 1000) or nanoseconds (multiply by 10^9). Always verify the format before calculating.
Can the result be negative?
Yes. If the end timestamp is earlier than the start timestamp, the result is negative — indicating reverse chronological order.
Do leap years affect epoch subtraction?
No. Because epoch values count total elapsed seconds, leap years are already baked in. No extra logic is needed for subtraction — only when converting back to calendar dates.
How do I handle timestamps in different time zones?
You do not need to. Epoch timestamps are always UTC. Time zones only matter when displaying the result in human-readable form. Use a timezone-aware library for that step.
Subtract. Divide by 60. Double-check the unit. Three steps that separate a correct billing engine from a $340,000 mistake. The math is elementary — the discipline is everything.