How to use the date Command

In this tutorial, I go through the basics of using the date command on a Linux-based distribution.

How to use the date Command

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.

  • -d or --date= STRING will display the time described by “STRING“. For example, you can enter “1 month ago” to get a date from one month ago.
  • --debug adds a note to the parsed date and will send warnings to stderr.
  • -f or --file= DATEFILE is similar to the --date option but accepts a file instead. It will parse each line inside the specified file (DATEFILE).
  • -I or --iso-8601 FMT will output the date and time in the ISO 8601 format. FMT is optional and controls the precision of the date and time. For example, --iso-8601=ns or -Ins will show the date and time right down to the nanosecond.
  • -R or --rfc-email outputs the date and time in the RFC 5422 format.
  • --rfc-3339=FMT will output the date and time in the RFC 3339 format. FMT controls the precision of the date and time. For example, --rfc-3339=date will show just the date.
  • -s or --set= STRING sets the time as described by “STRING“.
  • -r or --reference= FILE will display the last modification of the specified file.
  • -u or --utc or --universal will 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.

Date

  • %a outputs the abbreviated weekday name. For example, Mon.
  • %A will output the full weekday name. For example, Monday.
  • %b prints the abbreviated name of the month. For example, Jan.
  • %B outputs the full month name, such as January.
  • %C is similar to %Y but only prints the first two numbers of a year. For example, 2022 will be 20.
  • %d will print the day of the month, such as 10 for the 10th day of the month.
  • %D prints the date. The format is the same as using %m/%d/%y.
  • %F is similar to %D, but the format is the same as using %Y-%m-%d.
  • %m prints the current month. So, March will be 03.
  • %y outputs the last two digits of the year. For example, 2022 will be 22.
  • %Y will output the current year.

Time

  • %H outputs the hour in 24-hour formatting. For example, 11 pm will be 23.
  • %I will print the hour in 12-hour formatting. So, 11 pm will be 11.
  • %M outputs the current minute, so 10:54:02 will be 54.
  • %r prints the locales 12-hour time. For example, 04:21:02 PM.
  • %S outputs the current second, so 10:54:02 will be 02
  • %T prints the current time in 24-hour time. It is the same as using %H:%M:%S.

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.

Using %D will print the date with the numbers separated by a backslash. It is the same as using %m/%d/%y.

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.

  • %A displays the day of the week.
  • %d will output the current day of the month.
  • %B outputs the full name of the month.
  • %Y will output the year.
dev@pimylifeup:~/ $ date +"%A %d %B %Y"
Monday 12 December 2022

Using %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 -s or --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 -r or --reference= option.

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.

date --date="yesterday"

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.

date --date="tomorrow"

The output will contain tomorrow’s date.

dev@pimylifeup:~/test$ date --date="tomorrow"
Tue 13 Dec 2022 04:36:00 AM UTC

More Help

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.

man date

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.

date --help

Conclusion

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.

Leave a Reply

Your email address will not be published. Required fields are marked *