Subscribe to the feed
Linux 

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:

date command with output displaying day, month, day of the month, hour:minute:second, time zone, and year

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

date command output after using the -u option, displaying 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:

Output of: date +"Year: %Y, Month: %m, Day: %d" with year, month, and day fields labelled.

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:

Output of date +"%D" output displays as 12/26/20

Here is another example:

Output from the date +"%D %T" command displays the date and time

This example reverses the results:

Output of the date +"%T %D" command, reversing the information 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:

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:

Output of the TZ='America/New_York' date command, displaying the time in the New York time zone

Run the following command to display the time in Tokyo:

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:

Output of date --date="next fri" displaying next Friday's date

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

Output of the date command displaying a future time and date in New York

Display past dates

You can also use date to display past information.

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:

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:

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.

Alias of a formatted date command in ~/.bashrc Output of the "today" alias displaying formatted date output

Set the format as a variable

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

Displays content of a script declaring a variable

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.

Template displaying 3 formatted date commands for use in scripts

[ 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?


About the author

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.

Read full bio
UI_Icon-Red_Hat-Close-A-Black-RGB

Browse by channel

automation icon

Automation

The latest on IT automation for tech, teams, and environments

AI icon

Artificial intelligence

Updates on the platforms that free customers to run AI workloads anywhere

open hybrid cloud icon

Open hybrid cloud

Explore how we build a more flexible future with hybrid cloud

security icon

Security

The latest on how we reduce risks across environments and technologies

edge icon

Edge computing

Updates on the platforms that simplify operations at the edge

Infrastructure icon

Infrastructure

The latest on the world’s leading enterprise Linux platform

application development icon

Applications

Inside our solutions to the toughest application challenges

Original series icon

Original shows

Entertaining stories from the makers and leaders in enterprise tech