In this tutorial, I go through the basics of using the date command on a Linux-based distribution.
You can use the date command to view the current date and time on your Linux system. The command allows you to format the output, view the last modified date on files, set the date and time, and more. It is an incredibly useful command which I highly recommend taking the time to learn.
There are many ways you can utilize this command, but you will likely need it when creating bash scripts that require the current date and time. You can also set the time using the command, but it is unlikely you will ever need to use this functionality.
This tutorial will take you through the syntax, options, and several examples of how you can use the date command.
date Command Syntax
The syntax for the date command is relatively simple and accepts two additional arguments. The example below demonstrates how you will need to structure the command.
date [OPTIONS] +[FORMAT]
- [OPTIONS] is where you can specify the options you want to use with the date command. We detail some options you can use further down the page.
- [FORMAT] is where you can specify how you want the output formatted. We will touch on a few examples further down the page.
date Command Options
There are a few different options that you can use with the date command. Below are some that you are likely to use.
STRINGwill display the time described by “
STRING“. For example, you can enter “
1 month ago” to get a date from one month ago.
--debugadds a note to the parsed date and will send warnings to stderr.
DATEFILEis similar to the
--dateoption but accepts a file instead. It will parse each line inside the specified file (
FMTwill output the date and time in the ISO 8601 format. FMT is optional and controls the precision of the date and time. For example,
-Inswill show the date and time right down to the nanosecond.
--rfc-emailoutputs the date and time in the RFC 5422 format.
--rfc-3339=FMTwill output the date and time in the RFC 3339 format.
FMTcontrols the precision of the date and time. For example,
--rfc-3339=datewill show just the date.
STRINGsets the time as described by “
FILEwill display the last modification of the specified file.
--universalwill output the coordinated universal time (UTC). You can also use this option to set the UTC time.
Formatting the Date & Time
You can specify the format on how you want the date and time to be displayed in the terminal. In this section, we briefly discuss some of the patterns you can use. Please see the command manual for all the possible variations.
%aoutputs the abbreviated weekday name. For example, Mon.
%Awill output the full weekday name. For example, Monday.
%bprints the abbreviated name of the month. For example, Jan.
%Boutputs the full month name, such as January.
%Cis similar to
%Ybut only prints the first two numbers of a year. For example, 2022 will be
%dwill print the day of the month, such as
10for the 10th day of the month.
%Dprints the date. The format is the same as using
%Fis similar to
%D, but the format is the same as using
%mprints the current month. So,
%youtputs the last two digits of the year. For example, 2022 will be
%Ywill output the current year.
%Houtputs the hour in 24-hour formatting. For example,
11 pmwill be
%Iwill print the hour in 12-hour formatting. So,
11 pmwill be
%Moutputs the current minute, so
%rprints the locales 12-hour time. For example,
%Soutputs the current second, so
%Tprints the current time in 24-hour time. It is the same as using
Using the date Command
This section will go through several examples of using the date command to perform various tasks. Of course, there are more tasks that you can complete using the date command, but these should give you a decent introduction.
Using the date command without options will return the current date and time. The example below demonstrates using the date command with no options.
dev@pimylifeup:~/ $ date Mon 12 Dec 2022 04:40:38 AM UTC
Formatting the Output
You can format the output of the date command by specifying a phrase after the plus (+) symbol. Earlier in this tutorial, we touched on some of the different inputs that you can use to format a date string. Below are a few examples that make use of these format strings.
%D will print the date with the numbers separated by a backslash. It is the same as using
dev@pimylifeup:~/ $ date +"%D" 12/12/22
In this example, we use a variety of format options. You can use a combination of options to format the date and time string to how you want it to be.
%Adisplays the day of the week.
%dwill output the current day of the month.
%Boutputs the full name of the month.
%Ywill output the year.
dev@pimylifeup:~/ $ date +"%A %d %B %Y" Monday 12 December 2022
%r will set it so that the time is outputted as a 12-hour time, as shown in the example below. Using
%T will output the time in 24-hour time.
dev@pimylifeup:~/ $ date +"%r" 04:58:13 AM
Change the Date and Time
You can change the system date and time using either the
--set= options. You can specify the timestamp exactly or use a phrase like “yesterday”. You will also need to use sudo to use the set option.
It is recommended that you use this option only if you understand what you are doing.
Below is an example of setting the date and time using the
-s option and a timestamp.
date -s "2022-03-21 07:28:12"
When the command runs correctly, you should see an output similar to the example below.
dev@pimylifeup:~$ sudo date -s "2022-03-21 07:28:12" Mon 21 Mar 2022 07:28:12 AM UTC
Below is an example of setting the date using the
-s option and a phrase. This phrase will set the date to yesterday’s date.
date -s "yesterday"
Running the above command will produce an output similar to the example below.
The date below the command should be yesterday’s date.
dev@pimylifeup:~$ sudo date -s "yesterday" Sat 10 Dec 2022 10:29:19 AM UTC
Last Modified Timestamp
You can view the last modified timestamp of a file by using the date command and the
In the example below, we use the
-r option to check the last modified date of the
home.php file in our current working directory.
date -r home.php
You should see the last modified timestamp in the terminal after entering the command.
dev@pimylifeup:~/test$ date -r home.php Thu 20 Oct 2022 10:41:04 AM UTC
View a Date in the Past
You can use the date command to view a date in the past. You can do this by using the
--date option and a phrase specifying the date you want to view.
date --date="1 year ago"
As you can see, the command returns the date from one year ago.
dev@pimylifeup:~/test$ date --date="1 year ago" Sun 12 Dec 2021 04:16:46 AM UTC
In our next example, we request yesterday’s date.
The output will contain yesterday’s date.
dev@pimylifeup:~/test$ date --date="yesterday" Sun 11 Dec 2022 04:22:46 AM UTC
View a Date in the Future
Like viewing a date in the past, you can also view a date in the future using the
--date option. Again, you will need to enter a phrase to specify the date you wish to view. Below are a few examples of viewing a date in the future.
The following example requests the date one year from today.
date --date="1 year"
The output will display the date from next year.
dev@pimylifeup:~/test$ date --date="1 year" Tue 12 Dec 2023 04:34:49 AM UTC
In this example, we request tomorrow’s date.
The output will contain tomorrow’s date.
dev@pimylifeup:~/test$ date --date="tomorrow" Tue 13 Dec 2022 04:36:00 AM UTC
If you require more help with using this command, there are several different methods that you can use to get more information.
The first method that I recommend using is the man command. The man command lets you view the manual pages of the given command. This command is included in most Linux distributions.
The other alternative is to use the
--help option with the command. Using this option will have the command print useful information into the terminal.
You should now have a decent idea of how you can use the date command to output the time and date into the terminal. You may also find it useful in bash scripts.
There is a huge number of Linux commands that you can use to perform a huge variety of tasks on a Linux-based distribution. Therefore, I highly recommend learning as many as possible if you plan on administrating any Linux system.
Please let us know if you notice a mistake or if an important topic is missing from this guide.