Mastering the Linux date Command: Formats, Options, and Real‑World Examples
This guide explains how to use the Linux date command to display, format, and manipulate dates and times, covering options such as –date, –file, –set, –u, –r, relative dates, and custom format strings, with practical code examples for each scenario.
The date command displays the current date and time in various formats and can also set the system date and time. This article demonstrates common usages and practical examples.
1. Using the –date option to display a date from a string
Provide a static date or time string with -d or --date to have date parse and output it. If no time is given, 00:00:00 is assumed.
$ date --date="12/2/2014"
Tue Dec 2 00:00:00 PST 2014
$ date --date="2 Feb 2014"
Sun Feb 2 00:00:00 PST 2014
$ date --date="Feb 2 2014"
Sun Feb 2 00:00:00 PST 2014When both date and time are supplied, they are displayed accordingly.
$ date --date="Feb 2 2014 13:12:10"
Sun Feb 2 13:12:10 PST 20142. Using the –file option to read date patterns from a file
The -f or --file option processes each line of a file as a date string.
$ cat datefile
Sept 9 1986
Aug 23 1987
$ date --file=datefile
Tue Sep 9 00:00:00 PDT 1986
Sun Aug 23 00:00:00 PDT 19873. Using –date for relative dates
Relative expressions such as "next mon" or "3 seconds ago" can be used.
$ date --date="next mon"
Mon May 27 00:00:00 PDT 2013
$ date --date="@5"
Wed Dec 31 16:00:05 PST 1969
$ date --date="@10"
Wed Dec 31 16:00:10 PST 1969
$ date --date="@60"
Wed Dec 31 16:01:00 PST 19694. Displaying past dates
Use expressions like "3 seconds ago", "1 day ago", "yesterday", "1 month ago", or "1 year ago".
$ date --date='3 seconds ago'
Mon May 20 21:59:20 PDT 2013
$ date --date="1 day ago"
Sun May 19 21:59:36 PDT 2013
$ date --date="yesterday"
Sun May 19 22:00:26 PDT 2013
$ date --date="1 month ago"
Sat Apr 20 21:59:58 PDT 2013
$ date --date="1 year ago"
Sun May 20 22:00:09 PDT 20125. Using –set to set the system date and time
The -s or --set option changes the system clock.
$ date
Sun May 20 20:09:31 PDT 2013
$ date -s "Sun May 20 21:00:00 PDT 2013"
Sun May 20 21:00:00 PDT 2013
$ date
Sun May 20 21:00:05 PDT 20136. Using –u to display UTC
Show the date in Coordinated Universal Time with -u, -utc, or -universal.
$ date
Mon May 20 22:07:53 PDT 2013
$ date -u
Tue May 21 05:07:55 UTC 20137. Using –r to display a file's last modification time
First, modify the file's timestamp with touch, then use -r to show it.
$ date
Sun May 20 20:25:48 PDT 2013
$ touch datefile
$ date
Sun May 20 20:26:12 PDT 2013
$ date -r datefile
Sun May 20 20:25:57 PDT 20138. Various date format options
The +%<format> syntax lets you customize the output. Common options include: date +%a – short weekday name (e.g., Thu) date +%A – full weekday name (e.g., Thursday) date +%b – short month name (e.g., Feb) date +%B – full month name (e.g., February) date +%d – day of month (01‑31) date +%D – date in MM/DD/YY date +%F – date in YYYY‑MM‑DD date +%H – hour (00‑23) date +%I – hour (01‑12) date +%j – day of year (001‑366) date +%m – month (01‑12) date +%M – minute (00‑59) date +%S – second (00‑60) date +%N – nanoseconds (000000000‑999999999) date +%T – time as HH:MM:SS (24‑hour) date +%u – day of week (1‑7, Monday=1) date +%U – week number (Sunday first) date +%Y – full year (YYYY) date +%Z – time‑zone abbreviation (e.g., EDT)
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
