
The date command in Linux is used to display or set system date and time. It allows users to display time in various formats and calculates the past and future dates.
In this tutorial, we learn about the date command in Linux with usage examples.
How to use the Linux date command?
The following is the syntax of date command on the Linux terminal:
date [option]... [+format]
By default the date command prints the date in the timezone in which the system is configured. To change the date and time you should need a user with root or sudo privilege.
Display Date
When the date command is used without any options and arguments, it displays the current date and time in the default format as follows:
$ date

The output shows the day of the week, month’s day, current month, year, the current time, and timezone with year. The date command displays the default time zone that is set on your operating system.
Show Future dates
The date string -d or --date option allows you to print the future or upcoming dates. To print the future dates, type the values into the strings such as “next Friday”, “tomorrow” or similar, etc.
For example, to print tomorrow’s date, type the following command:
$ date -d ”tomorrow”

Display the next Friday date:
$ date -d "next Friday"

Display past dates
Using the -d option, you can display the past dates on your Linux system. The date string allows you to enter the values such as “yesterday”, “Saturday”, “last Friday”, “3 year ago” or similar. So, by using the following date string, you can display past dates:
$ date -date "1 year ago"

To display the yesterday date, use the following string with the date command:
$ date -d ”yesterday”

Similarly, to display the last Friday date, run the below-mentioned command:
$ date -d ”last Friday”

Date Command Format Options
The displayed terminal output of the Linux date command can be formatted using format control characters which are preceded by a ‘+’ symbol. All these format controls proceed with the ‘%’ sign and are followed by their values.
For example, we want to format the output of the date command in the following way:
$ date +"Year: %Y, Month: %m, Day: %d"

Using the following most common list of formatting characters, you can display output in your desired format:
- %a - Prints weekday’s name in short format (e.g., Mon)
- %A - Used to display full weekday name (e.g., Monday)
- %b - Display the name of the month in short form (e.g., Jan)
- %B - Used to display the full month’s name (e.g., January)
- %d - Displays the month’s day (e.g., 05)
- %H - Display Hour (00..23)
- %I - Display Hour in (01..12) format
- %j - Displays the Day of year (001..366)
- %m - Displays Month in number (01..12)
- %M - Print Minutes in 00..59 sec.
- %S - Displays seconds (00..60)
- %u - Display week’s day in number (1..7)
- %Y - Used to display Full-year (e.g., 2019)
Using the following command, you can explore the full list of format options of the date command:
$ date --help
$ man date
Display Date from a String Value
The -date or -d options allow you to display the specific date. To display a specific date from a date string, specify a date string that is in the human-readable format like the following command:
$ date -d "2020-10-09 10:22:47"

Using the -d option, you can also display the date in the custom formatting as follows:
$ date -d '12 Jan 2021' +'%A, %d %B %Y'

Show the Last Modification Time of a File
Sometimes, we need to view the last modification time of a file. The date command in the Linux system helps you to print the last time of modification of a file. When you use the date command followed by the ‘-r’ option, it prints the last modification time of a file.
For example, to display the last modification date of the ‘/etc/hosts’ file, use the below-mentioned command:
$ date -r /etc/hosts

Date command to set the timezone
The date command displays your system default timezone that is defined in the ‘/etc/localtime’ file. However, to print a timezone of a different country set the ‘TZ’ environment variable to the desired timezone.
To view all available timezone lists by running the following command:
$ timedatectl list-timezones

For example, to set the timezone to display the Sydney to Australia time, run the following command:
$ TZ='Australia/Melbourne' date

Using as an Epoch Converter
The date command also works as an epoch converter. The epoch displays the time in the number of seconds that have elapsed since February 2, 1975, at 00:00:00 UTC. To display the elapsed time in seconds to the current date, use the date command along with the format control %s:
$ date +%s

For example, to view how many seconds elapsed from an epoch to a specific date, use the following command:
$ date -d "1980-10-25" +"%s"

Using date with other commands
The ‘date’ command in the Linux system can be implemented with other commands to create various filenames that contain information about the current date and time.
We have created in the following example a new SQL backup file by using the below-given command:
$ mysqldump database_name > database_name-$(date +%Y%m%d).sql
The date command can also be used in the shell script. In the following example, we assigned the date command output to a new variable ‘date_now’.
$ date_current=$(date "+%F-%H-%M-%S")
$ echo $date_current

Set Date in Linux
Using the date command, you can manually set the date of the Linux system.
For example, to manually set the date and time of the Linux system to 2:30 PM, October 13, 2021. Run the below-mentioned command:
$ date --set="20211013 05:30"

Most Linux distributions system time is by default synchronized with NTP or timedatectl / timesyncd. So no need to worry much about changing it.
Conclusion
We provided a tutorial on how to use the date command on a Linux system. To explore more about the date command please visit the date command man pages.