Maintaining accurate date on a Linux system is one of the essential skills that any Linux user should have at their fingertips. The Linux date command is used to display and set the date and time settings on a Linux system. This tutorial gives you a glimpse of how you can use the date command to display and set the date on your Linux system.
The basic syntax of the date command is provided below
$ date [option]… [+format]
1 ) Date command without options
In its basic form, without any command options, the date command displays your current date and time including the day of the week, month, year, the time in hh:mm:ss format, and the timezone as presented below.
$ date Sun May 9 15:41:17 IST 2021 $
2) Display the date and time in UTC
To display time in UTC (Coordinated Universal Time ),previously referred to as GMT (Greenwich Mean Time), append the -u option.
$ date -u Sun May 9 10:11:59 UTC 2021 $
3 ) Display a specific date in a string format
You can display a specific date in a string format using the –date option as illustrated below. Take note that this does not affect your system’s date and time, rather it simply converts the date format to a string.
$ date --date="10/10/2021" Sun Oct 10 00:00:00 IST 2021 $
4) Use the date command to check past dates
The date command can also print the date and time in the past relative to your current date.
For example, to check what the date was 9 days ago run the command.
$ date --date="9 days ago" Fri Apr 30 15:45:28 IST 2021 $
To view the date two weeks earlier, run the command:
$ date --date="2 weeks ago" Sun Apr 25 15:47:11 IST 2021 $
To check the date three months ago, execute:
$ date --date="3 months ago" Tue Feb 9 15:47:52 IST 2021 $
To print the date 3 years ago, run:
$ date --date="3 years ago" Wed May 9 15:49:04 IST 2018 $
5) Use the date command to check future dates
Just as you can check past dates, the date command also allows you to display future dates.
For example, to check tomorrow’s date run:
$ date --date="tomorrow" Mon May 10 15:50:14 IST 2021 $
To check what the date will be exactly a week from now, run:
$ date --date="next week" Sun May 16 15:51:03 IST 2021 $
To display what the date will be in two weeks’ time execute:
$ date --date="2 weeks" Sun May 23 15:51:46 IST 2021 $
To display the date 4 months from now run:
$ date --date="4 months" Thu Sep 9 15:52:33 IST 2021 $
And to check the date 2 years from now run:
$ date --date="2 years" Tue May 9 15:53:20 IST 2023 $
6) Date formatting options
The date command comes with many options that allow you to customize the date output. Listed below are some of the formatting options available.
- %D – Display date in the format mm/dd/yy
- %Y – Year (e.g., 2021)
- %m – Month (01-12)
- %B – Month name in the full string format (e.g., February)
- %b – Month name in the shortened string format (e.g., Feb)
- %d – Day of month (e.g., 01)
- %j – Day of year (001-366)
- %u – Day of the week (1-7)
- %A – Weekday in full string format(e.g., Friday)
- %a – Weekday in shortened format (e.g., Fri)
- %H – Hour (00-23)
- %I – Hour (01-12)
- %M – Minute (00-59)
- %S – Second (00-60)
The syntax for using the date option is quite simple:
$ date “+%option”
For example, to print the date in yy/mm/dd format, run
$ date "+%Y-%m-%m" 2021-05-05 $
To print the day of the week, month, year, and current time run:
$ date "+%A %B %Y %T" Sunday May 2021 15:55:56 $
7) How to set the date and time
The date command also allows you to set the date and time. For example, to set the date and time to 25, June 2021 at 11:15 am, run the command:
$ sudo date --set="20210625 11:15" Fri Jun 25 11:15:00 IST 2021 $
Setting the system’s date and time this way is discouraged since the time is most likely to be inaccurate. A better way to achieve this is to use the chrony utility which is a replacement for the now deprecated ntpd daemon. In fact, modern systems such as CentOS 8 / RHEL 8 don’t support NTP. We have a detailed guide on how to sync time and date using chrony.
In case you want to change time zone then use timedatectl command, example is shown below.
$ sudo timedatectl set-timezone Asia/Kolkata
8) Using Date command in variable
Sometime while creating a shell script, we save date command to a variable and then later we create log file using that variable, examples is shown below,
#!/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 "nn" >> $LOGFILE echo "##Check Cluster for Stopped Resources##" >> $LOGFILE crm_mon -1 -rf | grep -i STOPPED >> $LOGFILE echo -e "nn" >> $LOGFILE
We hope we have shed light on the use of the Linux date command, and we trust you can use it to display the system’s date and customize the output to your preference.
Also Read : 9 tee Command Examples in Linux
The post 8 Quick Date Command Examples in Linux first appeared on LinuxTechi.#news