Formatting tricks for the Linux date command
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:
One modification for the
date command is the
-u option. This option converts the output to Coordinated Universal Time (UTC). Here is an example:
[ You might also enjoy: Using at for single-use cron jobs in Linux ]
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:
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:
Here is another example:
This example reverses the results:
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:
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:
Run the following command to display the time in Tokyo:
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:
Here is an example that displays the local time for 10 AM next Friday on the east coast of the US:
Display past dates
You can also use
date to display past information.
This example shows time and date information from 15 days ago:
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.
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:
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
Set the format as a variable
You can also set the
date configuration as a variable on your system.
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.
[ A free course for you: Virtualization and Infrastructure Migration Technical Overview. ]
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?