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.
Date command in Linux
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.
The following is the syntax of date command on the Linux terminal:
date [option]... [+format]
Date command Examples in Linux
Let's go through some of the useful date command with examples.
1. 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:
Fri 24 Jun 2022 12:42:21 AM UTC
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.
2. 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”
Sat 25 Jun 2022 12:53:24 AM UTC
Display the next Friday date:
date -d "next Friday"
Fri 01 Jul 2022 12:00:00 AM UTC
3. 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"
Thu 24 Jun 2021 12:56:25 AM UTC
To display the yesterday date, use the following string with the date command:
date -d ”yesterday”
Thu 23 Jun 2022 12:56:52 AM UTC
Similarly, to display the last Friday date, run the below-mentioned command:
date -d ”last Friday”
Fri 17 Jun 2022 12:00:00 AM UTC
4. 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"
Year: 2022, Month: 06, Day: 24
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)
Display Format Examples
Let's check examples for time output in specific formats.
Display output in ISO 8601 format
Display output in RFC 3339 format
date --rfc-3339=seconds date --rfc-3339=minutes
Output in the desired format
date +"Current Year: %Y, current Month: %m, Day: %d, Current Week %V"
Output displaying the current year, month, day, and week fields.
Current Year: 2022, current Month: 06, Day: 24, Current Week 25
Using the following command, you can explore the full list of format options of the date command:
$ date --help $ man date
5. 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"
Fri 09 Oct 2020 10:22:47 AM UTC
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'
Tuesday, 12 January 2021
6. 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
Fri 19 Feb 2021 11:10:33 AM UTC
7. 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:
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"
8. 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:
sudo date --set="20211013 05:30"
Wed Oct 13 05:30:00 AM UTC 2021
In most Linux distributions the system time is by default synchronized with NTP or timedatectl / timesyncd. So no need to worry much about changing it.
9. 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
$ date_current=$(date "+%F-%H-%M-%S")
We provided a tutorial on how to use the date command on a Linux system. To explore more about the date command browse the date command man pages.