Operations 5 min read

Mastering the Linux date Command: Formats, Calculations & Timestamp Conversions

This guide explains why accurate date‑time handling matters for tasks like log naming, introduces the common date format specifiers, and provides step‑by‑step shell examples for retrieving the current time, custom‑formatted dates, relative dates such as yesterday or one hour ago, and converting between human‑readable dates and Unix timestamps.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering the Linux date Command: Formats, Calculations & Timestamp Conversions

Overview

The date utility on Linux prints the current date and time. Its default output is often unsuitable for scripts, log filenames, or data pipelines, so custom formatting and relative calculations are frequently required.

Basic invocation

date

Without arguments date prints the current time in the system’s locale (e.g., Sat Nov 3 22:01:57 CST 2018).

Format specifiers

When the +"format" argument is supplied, date substitutes the following specifiers (case‑sensitive): %Y – four‑digit year (e.g., 2023) %m – month number with leading zero ( 0112) %d – day of month with leading zero ( 0131) %H – hour in 24‑hour format ( 0023) %M – minute ( 0059) %S – second ( 0059)

Custom formatted output

date +'%Y-%m-%d'
# 2023-11-03

date +'%Y/%m/%d %H:%M:%S'
# 2023/11/03 14:08:14

Quotes are required to prevent the shell from interpreting the percent signs.

Relative dates with -d

The -d (or --date) option accepts a human‑readable description and computes the corresponding time.

# Yesterday at the same time
date -d 'yesterday' +'%Y/%m/%d %H:%M:%S'
# 2023/11/02 22:24:31

# Alternative syntax: "today -1 day"
date -d 'today -1 day' +'%Y-%m-%d'
# 2023-11-02

# One hour ago
date -d 'today -1 hour' +'%Y%m%d%H'
# 2023110214

date +'%Y-%m-%d %H:%M:%S' -d '-1 hours'
# 2023-11-02 14:43:38

# Day before a specific date (e.g., 2023‑10‑01)
date -d '20231001 -1 day' +'%Y%m%d'
# 20230930

Converting between human‑readable dates and Unix timestamps

To obtain a POSIX timestamp (seconds since 1970‑01‑01 UTC):

date -d "Nov  4 15:49:41 CST 2023" +%s
# 1709730581

To convert a timestamp back to a readable date, prefix the number with @:

date -d @1709730581
# Mon Nov  4 15:49:41 CST 2023

Formatting a timestamp directly:

date -d @1709730581 +' %Y%m%d %H:%M:%S'
# 20231104 15:49:41

Practical notes

All format strings are case‑sensitive; %Y and %y produce different results.

The -d parser follows GNU date syntax; on BSD/macOS the equivalent is date -v with different flags.

Timezone information (e.g., CST) is taken from the system’s locale unless overridden with TZ= environment variable.

When embedding the command in scripts, prefer single quotes to avoid shell expansion of $ or backticks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

timestampBashtime formattingdate command
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.