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.
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
dateWithout 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 ( 01 ‑ 12) %d – day of month with leading zero ( 01 ‑ 31) %H – hour in 24‑hour format ( 00 ‑ 23) %M – minute ( 00 ‑ 59) %S – second ( 00 ‑ 59)
Custom formatted output
date +'%Y-%m-%d'
# 2023-11-03
date +'%Y/%m/%d %H:%M:%S'
# 2023/11/03 14:08:14Quotes 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'
# 20230930Converting 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
# 1709730581To convert a timestamp back to a readable date, prefix the number with @:
date -d @1709730581
# Mon Nov 4 15:49:41 CST 2023Formatting a timestamp directly:
date -d @1709730581 +' %Y%m%d %H:%M:%S'
# 20231104 15:49:41Practical 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
