Mastering Date and Time Display Commands on Linux

The Linux date command is exceptionally flexible. With minimal effort, you can use it to display dates, times, and select date-related values such as the day of the week or time zone. The default output of the date command will show the current date and time in a detailed string that includes the day of the week, the current time, and the time zone. Here’s an illustration:

To see details of a date in the past or future, use the -d option. For instance, if you’re curious about which day of the week your birthday will fall on next year, you could use a command like this:

If you want to display a specific component or version of a date, use the characters outlined in the list of options below.

To apply these options with the date command, precede them with a + sign. For instance, the command below displays the date in mm/dd/yy format:

To use more than one option at a time, enclose the arguments in quote signs like this command that shows the day of week and date:

A command like the one below will display only the day of the week, but fully spelled out:

If you want the month spelled out fully, use a command like this one:

The date command also allows you to show dates in the future. While you likely know what day tomorrow is, you could use a similar command to display the current time but the next day.

This next command shows the day of the week 2 years in the future.

The next command shows the full date.

You can also use the date command to display dates and times in the past. The commands below show yesterday’s date, the date and time two hours ago, and the day of the week from two years ago today.

If you want to display date and time information in a multi-line display, insert a newline (%n) string into your date command where the break should occur. Here’s an example:

If you want to insert date/time information into files, use > to create/overwrite the file or >> to add the data as in these examples:

The TZ option will allow you to specify the time zone that you want to see displayed. So, if you want to see what time it is on the US West Coast when you’re on the US East Coast, you could use a command like this:

Time zones are defined for hundreds of locations. Here are a few examples:

To list available time zones (about 600 of them), you can use a command like one of those shown below. Keep in mind, however, that this command shows time zones for each city or area. This level of detail is generally more than needed since most cities also share more generic time zones with many other cities.

You can also use the three-character time zone abbreviations when they are available. Here are some examples:

The most unusual way to display the current time on Linux is to use the date +%s command. This shows the current time as the number of seconds since 00:00:00 on Jan 1, 1970, and is also referred to as the “epoch time”. Since this was designated to be Linux’s birth date and is the way dates are stored internally, it works quite well.

To see how many seconds have passed since a particular date, use a command like this:

Don’t overlook the fact that you can turn any date command into an alias if it saves you time and trouble. If you frequently need to display the current date and time in some other location, add an alias to your .bashrc file like this:

Once you source your .bashrc file or log in next time, it should work. I found a list of time zone abbreviations at this URL: https://www.timeanddate.com/time/zones/

The date command provides lots of ways to see dates and time, presented in various formats, in the past or future, and depending on location. For more posts related to dates and times, check out:

Total
0
Shares
Leave a Reply

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

Previous Article

AT&T Pays $370,000 to Hacker to Erase Compromised Phone Records

Next Article

Flock Review: Embracing Creature Comforts in a Cozy Simulator

Related Posts