Site icon GIXtools

Displaying dates and times on Linux

Displaying the current date

The Linux date command is extremely versatile. You can use it to display dates, times and select date-related values (like the day of the week or time zone) with very little effort. The default date command output will display the current date and time in a fairly detailed string that includes the day of the week, the current time and the time zone. Here’s an example:

$ date
Thu Jul  4 10:58:54 AM EDT 2024

Seeing date information for any date

If you want to see details on a date in the past or in the future, use the -d option. For example, maybe you want to know what day of the week your birthday is going to fall on next year. You could use a command like this:

$ date -d "2025-11-18"
Tue Nov 18 12:00:00 AM EST 2025

Single-value options

If you want to see a particular version or element of a date, use the characters described in the list of options below.

%D – Date as mm/dd/yy
%Y – Year (e.g., 2020)
%m – Month (01-12)
%B – Long month name (e.g., November)
%b – Short month name (e.g., Nov)
%d – Day of the month (e.g., 01)
%j – Day of the year (001-366)
%u – Day of the week (1-7)
%A – Full weekday name (e.g., Friday)
%a – Short weekday name (e.g., Fri)
%H – Hour (00-23)
%I – Hour (0-12)
%M – Minute (00-59)
%S – Second (00-60)

To use one of these options with the date command, precede it with a + sign. For example, the command below shows the date in mm/dd/yy format:

$ date +%D
07/04/24

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:

$ date +"%a %D"
Thu 07/04/24

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

$ date +%A
Thursday 

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

$ date +"%B %d"
July 04

Looking forward in time

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.

$ date --date="tomorrow"
Fri Jul  5 11:12:11 AM EDT 2024

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

$ date +%A --date "2 year"
Saturday

The next command shows the full date.

$ date --date="2 year"
Sat Jul  5 11:14:07 AM EDT 2026

Looking backward in time

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.

$ date --date="yesterday"
Wed Jul  3 11:19:02 AM EDT 2024

$ date --date "2 hour ago"
Thu Jul  4 09:21:23 AM EDT 2024

$ date +%A --date "2 year"
Saturday

$ date --date="+7 days ago"
Thu Jun 27 09:23:07 AM EDT 2024

Multi-line date info

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:

$ date "+DATE: %D%nTIME: %T"
DATE: 07/04/24
TIME: 11:38:44

Adding date info to files

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:

$ date "+DATE: %D%nTIME: %T" > Report
$ cat Report
DATE: 07/04/24
TIME: 11:43:35

Getting date/time information for other time zones

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:

$ TZ='America/Los_Angeles' date
Thu Jul  4 08:46:32 AM PDT 2024

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

Europe/Paris
Africa/Casablanca
Asia/Tokyo

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.

$ timedatectl list-timezones | head -4
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
$ timedatectl list-timezones | grep America | head -4
America/Adak
America/Anchorage
America/Anguilla
America/Antigua

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

$ TZ=EST date
Thu Jul  4 10:49:49 AM EST 2024
$ TZ=PDT date
Thu Jul  4 03:50:45 PM PDT 2024

Epoch time

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.

$ date +%s
1719936246

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

$ date -d "2000-11-11" +"%s"
973918800

Making it easier

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 .bashc file like this:

$ echo "alias Tokyo='TZ=Japan/Tokyo date'" >> ~/.bashrc

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/

Related how-tos

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:

Source:: Network World

Exit mobile version