Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

Unix Epoch Timestamp Guide for DevOps: Conversions, Commands, and Pitfalls

Originally published on 2012-02-22; fully revised and updated.

Unix timestamps show up everywhere in DevOps: logs, metrics, CI/CD events, audit records, and API payloads. This guide explains what epoch time is, how to convert it correctly across common tools, and what to watch for when timezone or unit mistakes can break debugging and automation.

Epoch and Unix timestamp illustration

What Unix epoch time means

A Unix timestamp (also called Unix time or POSIX time) is the number of seconds elapsed since 1970-01-01T00:00:00Z (UTC), not counting leap seconds. The moment 1970-01-01T00:00:00Z is epoch 0.

  • Epoch time is timezone-independent as a number.
  • Display formatting is timezone-dependent.
  • Many systems still store or exchange timestamps as integer seconds.

Time-unit reference for quick estimates

Human readable timeSeconds
1 hour3600
1 day86400
1 week604800
1 month (30.44 days, average)2629743
1 year (365.24 days, average)31556926

How epoch conversion works

In practice, conversion errors usually come from timezone assumptions or unit confusion (seconds vs milliseconds). A consistent flow helps avoid both.

flowchart TD
    A[Input value] --> B{Type of input}
    B -->|Human date| C[Choose timezone]
    C --> D[Convert to epoch seconds]
    B -->|Epoch number| E[Check unit]
    E -->|Seconds| F[Format as date time]
    E -->|Milliseconds| G[Divide by 1000]
    G --> F
    D --> H[Store compare or transmit]
    F --> H

Get the current Unix timestamp

These are common one-liners used in operations scripts and troubleshooting sessions.

EnvironmentCommandUnit returnedNotes
Unix or Linux shelldate +%sSecondsCommon in automation scripts.
Pythonint(time.time())Secondstime.time() is fractional seconds.
JavaScript or Node.jsMath.floor(Date.now()/1000)SecondsDate.now() is milliseconds.
PostgreSQLSELECT EXTRACT(EPOCH FROM now());SecondsUseful for SQL-side comparisons.
MySQLSELECT UNIX_TIMESTAMP(NOW());SecondsBehavior depends on DB timezone context.
PowerShell[DateTimeOffset]::UtcNow.ToUnixTimeSeconds()SecondsExplicit UTC source.
# Shell
date +%s

# UTC shell output (same epoch value, explicit display intent)
date -u +%sCode language: PHP (php)
import time
print(int(time.time()))Code language: JavaScript (javascript)
console.log(Math.floor(Date.now() / 1000));Code language: JavaScript (javascript)

Convert human-readable date and time to epoch

Always decide timezone first. If the input string does not include an offset, tools may assume local timezone or session timezone.

EnvironmentExampleTimezone behavior
GNU datedate -d “2024-01-01 00:00:00 UTC” +%sUTC explicitly provided in input.
macOS BSD datedate -j -u -f “%Y-%m-%d %H:%M:%S” “2024-01-01 00:00:00” +%s-u forces UTC interpretation.
Pythonint(datetime(2024,1,1,0,0,0,tzinfo=timezone.utc).timestamp())Explicit UTC via tzinfo.
JavaScriptMath.floor(new Date(“2024-01-01T00:00:00Z”).getTime()/1000)Z means UTC.
PostgreSQLSELECT EXTRACT(EPOCH FROM TIMESTAMPTZ ‘2024-01-01 00:00:00+00’);Offset included in value.
MySQLSELECT UNIX_TIMESTAMP(‘2024-01-01 00:00:00’);Typically interpreted using session timezone.
# GNU date example
date -d "2024-01-01 00:00:00 UTC" +%s

# macOS BSD date example
date -j -u -f "%Y-%m-%d %H:%M:%S" "2024-01-01 00:00:00" +%sCode language: PHP (php)
from datetime import datetime, timezone
print(int(datetime(2024, 1, 1, 0, 0, 0, tzinfo=timezone.utc).timestamp()))Code language: JavaScript (javascript)
-- PostgreSQL
SELECT EXTRACT(EPOCH FROM TIMESTAMPTZ '2024-01-01 00:00:00+00');

-- MySQL
SELECT UNIX_TIMESTAMP('2024-01-01 00:00:00');Code language: JavaScript (javascript)

Convert epoch to human-readable date and time

When converting back, choose whether you want UTC output or local timezone output for operators and dashboards.

EnvironmentExampleOutput style
GNU datedate -u -d @1704067200 “+%Y-%m-%d %H:%M:%S UTC”UTC text output.
macOS BSD datedate -u -r 1704067200 “+%Y-%m-%d %H:%M:%S UTC”UTC text output.
Pythondatetime.fromtimestamp(1704067200, tz=timezone.utc)Timezone-aware datetime object.
JavaScriptnew Date(1704067200 * 1000).toISOString()ISO 8601 UTC string.
PostgreSQLSELECT to_timestamp(1704067200);Timestamp value based on epoch input.
MySQLSELECT FROM_UNIXTIME(1704067200);Rendered in DB timezone context.
# GNU date
date -u -d @1704067200 "+%Y-%m-%d %H:%M:%S UTC"

# macOS BSD date
date -u -r 1704067200 "+%Y-%m-%d %H:%M:%S UTC"Code language: PHP (php)
from datetime import datetime, timezone
print(datetime.fromtimestamp(1704067200, tz=timezone.utc).isoformat())Code language: JavaScript (javascript)
console.log(new Date(1704067200 * 1000).toISOString());Code language: JavaScript (javascript)

Seconds vs milliseconds: quick comparison

Timestamp unitTypical length todayCommon sourcesConvert to seconds
Seconds10 digitsdate +%s, many SQL functionsUse as-is
Milliseconds13 digitsJavaScript Date.now(), many APIsDivide by 1000

If an epoch value looks too large, confirm unit before parsing. A millisecond value treated as seconds usually produces dates far in the future.

Y2038 and integer-size considerations

Systems that store epoch seconds in a signed 32-bit integer can overflow in January 2038 (the Y2038 issue). Modern platforms commonly use wider integer types, but old schemas, legacy binaries, and embedded systems should still be reviewed.

Reference and original links

Original article: How to Understand Epoch and Unix Timestamp

Common pitfalls and best practices

  • Store canonical timestamps in UTC-based epoch seconds or milliseconds, then format for users at the edge.
  • Document timestamp unit in every API and schema field name where possible.
  • When parsing date strings, include timezone offsets explicitly instead of relying on defaults.
  • In SQL, verify session and server timezone settings before comparing epoch and datetime fields.
  • For cross-platform shell scripts, account for GNU date and BSD date option differences.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Team Foundation Server 2015 Installation Guide (SQL Express + Update 3)

Install Team Foundation Server 2015 using the original Microsoft download paths, with clear guidance for online, offline, and ISO-based setup. This revised guide also includes an installation workflow, command example, and practical pitfalls to avoid.

Read More

How to Access a Running Docker Container Shell: docker exec vs docker attach

Need a shell inside a running container that is already in detached mode? This updated guide shows the correct workflow, compares docker exec and docker attach, and explains when SSH does and does not make sense.

Read More

Host Your Own NuGet Feed: Local Source vs NuGet.Server vs NuGetGallery

Learn how to host a private NuGet feed using three practical approaches: a local folder source, NuGet.Server, and NuGetGallery. This revised guide includes setup steps, command examples, and a workflow diagram to help you choose the right model.

Read More

Publish NuGet Packages to NuGet.org: CLI Steps, Workflow, and CI Example

Learn how to publish a NuGet package to NuGet.org using either nuget.exe or the dotnet CLI. This revised guide includes a workflow diagram, command comparison, and a CI-friendly example.

Read More

How to Create NuGet Packages with nuget.exe (Assembly, .nuspec, and .csproj Methods)

Learn three practical ways to build NuGet packages using nuget.exe: from an existing assembly, from a convention-based folder layout, or directly from a Visual Studio project. This guide includes commands, folder conventions, token behavior, and common pitfalls.

Read More

Virtualization in DevOps: Using Vagrant, Docker, and Cloud Platforms Together

This guide explains how VM-based workflows, containers, and cloud platforms complement each other in DevOps. It includes a practical workflow, trade-off table, and command examples for day-to-day use.

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x