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.


  • [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.


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.


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.

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.


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.


When the command runs correctly, you should see an output similar to the example below.


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.


Running the above command will produce an output similar to the example below.

The date below the command should be yesterday’s date.


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.


You should see the last modified timestamp in the terminal after entering the command.


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.


As you can see, the command returns the date from one year ago.


In our next example, we request yesterday’s date.


The output will contain yesterday’s date.


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.


The output will display the date from next year.


In this example, we request tomorrow’s date.


The output will contain tomorrow’s date.


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.


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.


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.

Print Friendly, PDF & Email

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Server Status

Aradippou Chat 1 Chat 2 Larnaca Limassol Nicosia

Chat Links

Official Links.

1. Chat 1.
2. Chat 2.

Alternative Mirror Links.

1. KiwiIRC 1.
2. KiwiIRC 2.

Other Web Clients.

1. IrcCloud.

Recent Posts

Related Posts:

Advertisement

Archives

Super Club Radio

Mighty Deals

CyIRC

CyIRC Tweets

Scroll Up
Fatal error: Uncaught WMAC\JSMin_UnterminatedStringException: WMAC\JSMin: Unterminated String at byte 1180: "+o.accessToken}}).then(e=>{if(!e.ok)throw e;return e});return a.catch(e=>{console.error(`Failed to logout chatrix session. deviceId: ${o.deviceId}`)}),a}})(); in /home/bszucbfe/public_html/wp-content/plugins/clearfy/components/minify-and-combine/includes/classes/ext/php/jsmin.php:215 Stack trace: #0 /home/bszucbfe/public_html/wp-content/plugins/clearfy/components/minify-and-combine/includes/classes/ext/php/jsmin.php(157): WMAC\JSMin->action(2) #1 /home/bszucbfe/public_html/wp-content/plugins/clearfy/components/minify-and-combine/includes/classes/ext/php/jsmin.php(96): WMAC\JSMin->min() #2 /home/bszucbfe/public_html/wp-content/plugins/clearfy/components/minify-and-combine/includes/classes/class-main.php(375): WMAC\JSMin::minify('(function(){"us...') #3 /home/bszucbfe/public_html/wp-includes/class-wp-hook.php(324): WMAC_PluginMain->jsSnippetcacher('(function(){"us...', '/home/bszucbfe/...') #4 /home/bszucbfe/public_h in /home/bszucbfe/public_html/wp-content/plugins/clearfy/components/minify-and-combine/includes/classes/ext/php/jsmin.php on line 215