Master the Linux date Command: Display, Format, and Set Dates
This tutorial explains how to use the Linux date command to show the current time, format dates in various ways, query past or future dates, customize output with format specifiers, set the system clock, and incorporate date values into shell scripts.
1. Display the current date and time
Running date without any options prints the current weekday, month, day, time, timezone and year, e.g.:
[root@server1 ~]# date
Mon May 24 14:50:31 CST 20212. Show UTC (Coordinated Universal Time)
Use the -u flag to display the time in UTC:
[root@server1 ~]# date -u
Mon May 24 06:51:21 UTC 20213. Display a specific date using a string
The --date option accepts a date string and prints the corresponding date without changing the system clock:
[root@server1 ~]# date --date="5/20/2021 13:14"
Thu May 20 13:14:00 CST 20214. Query past dates
You can ask for dates relative to now, such as "9 days ago", "3 months ago" or "2 years ago":
[root@server1 ~]# date --date="9 days ago"
Sat May 15 14:56:47 CST 2021
[root@server1 ~]# date --date="3 months ago"
Wed Feb 24 14:57:29 CST 2021
[root@server1 ~]# date --date="2 years ago"
Fri May 24 15:02:40 CST 20195. Query future dates
Similarly, you can request dates like "tomorrow", "next week", "3 weeks", "4 months" or "2 years":
[root@server1 ~]# date --date="tomorrow"
Tue May 25 14:58:49 CST 2021
[root@server1 ~]# date --date="next week"
Mon May 31 14:59:27 CST 2021
[root@server1 ~]# date --date="3 weeks"
Mon Jun 14 15:00:11 CST 2021
[root@server1 ~]# date --date="4 months"
Fri Sep 24 15:01:42 CST 2021
[root@server1 ~]# date --date="2 years"
Wed May 24 15:03:32 CST 20236. Formatting output with options
The date command supports a wide range of format specifiers. Some common ones are: %D – month/day/year (e.g., 05/24/21) %Y – four‑digit year (e.g., 2021) %m – month number (01‑12) %B – full month name (e.g., January) %b – abbreviated month name (e.g., Jan) %d – day of month (01‑31) %j – day of year (001‑366) %u – weekday number (1‑7, Monday=1) %A – full weekday name (e.g., Friday) %a – abbreviated weekday name (e.g., Fri) %H – hour (00‑23) %I – hour (01‑12) %M – minute (00‑59) %S – second (00‑60)
To use a format, pass it after a plus sign, for example:
date "+%Y/%m/%d"
2021/05/24Or to print a full readable timestamp:
date "+%A %B %d %Y %T"
Monday May 24 2021 15:24:077. Setting the system date and time
With the --set option you can change the system clock. For example, to set the date to 25 June 2021 11:15 AM:
[root@server1 ~]# date --set="20210625 11:15"
Fri Jun 25 11:15:00 CST 20218. Using date in shell scripts
Storing the command output in a variable is useful for generating log file names or timestamps. Example script:
#!/bin/bash
LOGFILE=/tmp/logs-$(date +%d-%m-%Y_%T)
echo "##Check Cluster for Failed Resources##" >> $LOGFILE
crm_mon -1 -rf | grep FAILED >> $LOGFILE
echo -e "
" >> $LOGFILE
echo "##Check Cluster for Stopped Resources##" >> $LOGFILE
crm_mon -1 -rf | grep -i STOPPED >> $LOGFILE
echo -e "
" >> $LOGFILEThis demonstrates how date can provide dynamic timestamps for log management and other automation tasks.
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.
