Skip to main content

Formatting tricks for the Linux date command

The Linux date command is simple, yet powerful. This article shows you how to unleash the power of the date command.
Image
Magic trick with Ace of Spades

Image by Pexels from Pixabay

The date command is simple. However, it has several useful options that enhance it. It's also capable of giving you practical information about past or future dates. This article shows you some of the format controls to manipulate the date command's output. At the end of the article, I offer some practical suggestions about how you can use this command in conjunction with common tasks.

Basic syntax for date

The most basic syntax for the date command is simply to type in the command with no options and no format controls. Here is an example of the command and its resulting output:

Image
date command with output displaying day, month, day of the month, hour:minute:second, time zone, and year
The date command's basic syntax.

One modification for the date command is the -u option. This option converts the output to Coordinated Universal Time (UTC). Here is an example:

Image
date command output after using the -u option, displaying UTC time information.
The date command with the -u option displays UTC time information.

[ You might also enjoy: Using at for single-use cron jobs in Linux ]

Format controls

The real customization of the date command occurs when you start adding format controls. The controls can order output fields, manage abbreviations, set the format of the date on the screen or in a file, etc. At the end of the article, I show some examples of how you might use date, and you'll see how controlling the format is useful.

Basic format control syntax

Use one or more format controls to display information. Here is the general syntax:

# date +%<format-option>

Let's look at a few examples.

Label the output fields

If you want the output labeled, you can use this format:

Image
Output of: date +&quot;Year: %Y, Month: %m, Day: %d&quot; with year, month, and day fields labelled.
Output displaying the year, month, and day fields labeled.

Change the order of the fields

You can alter the order in which the fields are displayed. This is one of the most useful customizations of date because it allows you to display the output in whatever format is most useful or familiar to you. Here is one way to do it:

Image
Output of date +&quot;%D&quot; output displays as 12/26/20
Output from the date +"%D" displays the month, day, and year separated by / characters.

Here is another example:

Image
Output from the date +&quot;%D %T&quot; command displays the date and time
Output from the date +"%D %T" command displays the date fields separated by / characters and the current time.

This example reverses the results:

Image
Output of the date +&quot;%T %D&quot; command, reversing the information from the previous example.
You may rearrange the information. In this case, the output is reversed from the previous example.

Use dashes, slashes, or spaces between the fields

Maybe you need to format the date output to meet particular standards, such as date information separated by dash, slash, or space characters. Here are a couple of different examples:

Image
Output of date with fields separated by - characters and then in the second example by / characters
Output of date with fields separated by - characters and then in the second example by / characters.

Display information from outside my current locale or time zone

It's embarrassing to say, but for whatever reason, time zones completely mess with my mind. They always have and they probably always will. That's why I was excited to discover that the date command can save me from having to visualize the sun's position over a particular geographical location to figure out the appropriate time zone.

First, you must know the name of the time zone you wish to check. You can use the timedatectl list-timezones command to display this information.

Next, combine the TZ value with the date command to display the time zone's time and date information. To show the time on the east coast of the US, run this command:

Image
Output of the TZ='America/New_York' date command, displaying the time in the New York time zone
TZ and date display the time in the specified time zone, while the current date output represents the configured time zone.

Run the following command to display the time in Tokyo:

Image
Output of the TZ='Pacific/Tokyo' date command, displaying the time in Tokyo, Japan
Output of the TZ='Pacific/Tokyo' date command, displaying the time in Tokyo, Japan.

Display future time/date

What if you're coordinating a meeting with someone on the east coast of the US, and you want to confirm or display a particular future time? You can use the date command to display that information.

First, you could display the date and time information for next Friday:

Image
Output of date --date=&quot;next fri&quot; displaying next Friday's date
Display the date of the next Friday on the calendar by using the date --date="next fri" command.

Here is an example that displays the local time for 10 AM next Friday on the east coast of the US:

Image
Output of the date command displaying a future time and date in New York
Output of the date --date='TZ="America/New_York" 10:00 next fri" that displays what the local time will be compared to 10:00 next Friday in New York.

Display past dates

You can also use date to display past information.

Image
Output from the date --date=&quot;5 year ago&quot; command showing the date and time from five years in the past.
Output from the date --date="5 year ago" command showing the date and time from five years in the past.


This example shows time and date information from 15 days ago:

Image
Output from the date --date=&quot;15 day ago&quot; command displaying time and date information from 15 days in the past
Output from the date --date="15 day ago" command displaying time and date information from 15 days in the past.

Practical application

It's one thing to know these tricks when using the date command, but it's another to understand how to apply them. Here are a few straightforward scenarios to get you thinking about your own tasks where date might be useful.

Redirect

You can redirect the date command into a text file. You can use any of the format controls above to customize the output. For example, what if you are conducting a very simple server documentation project? You might use the following commands:

Image
Output from a series of system inventory commands, including date.
Output from a series of system inventory commands, including date.

Set a date alias

Do you prefer the date and time to be displayed in a specific format different from the default? Set an alias for date that shows the information the way you like it. You can do this in your ~/.bashrc file.

Image
Alias of a formatted date command in ~/.bashrc
Set an alias for the string "today" in the ~/.bashrc file to display formatted date command output.
Image
Output of the &quot;today&quot; alias displaying formatted date output
Output of the "today" alias displaying the formatted date output.

Set the format as a variable

You can also set the date configuration as a variable on your system.

Image
Displays content of a script declaring a variable
Declare the "today" variable as $(date +%d/%m/%Y) for use in a script.

Create a template file, then copy/paste the content into scripts depending on the format desired

Perhaps you create or manage various scripts, and the date output is needed in several of them. However, the date format must be different in each script. You could create a master template file that stores the required formats and then copy/paste the appropriate template into your new scripts.

Image
Template displaying 3 formatted date commands for use in scripts
Create a template that stores commonly-used date command formats to be copied into scripts, etc.

[ A free course for you: Virtualization and Infrastructure Migration Technical Overview. ] 

Wrap up

Like many things in Linux, the date command is simple but also very flexible. You can control the format of the data that returned to you, ensuring you get the information you need as efficiently as possible. How have you used the date command to make your life easier?

Author’s photo

Damon Garn

Damon Garn owns Cogspinner Coaction, LLC, a technical writing, editing, and IT project company based in Colorado Springs, CO. Damon authored many CompTIA Official Instructor and Student Guides (Linux+, Cloud+, Cloud Essentials+, Server+) and developed a broad library of interactive, scored labs. He regularly contributes to Enable Sysadmin, SearchNetworking, and CompTIA article repositories. Damon has 20 years of experience as a technical trainer covering Linux, Windows Server, and security content. He is a former sysadmin for US Figure Skating. He lives in Colorado Springs with his family and is a writer, musician, and amateur genealogist. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.