Americas

  • United States
sandra_henrystocker
Unix Dweeb

Displaying dates and times on Linux

How-To
Oct 16, 20234 mins
Linux

Linux has a lot of options for displaying dates and times -- even for past and future dates. Take a look at how many ways you can elect to view this information.

Calendar pages flipping through months. [time / past / future / history / what's next]
Credit: MicroStockHub / Getty Images

Linux provides a lot of ways to display date and time information and not just for the current date and time. You can get information on dates way in the past or in the far future. You can also limit the data provided to just the current weekday or month. This post explains many of these options and provides examples of what you can expect to see.

Displaying the current date

Typing “date” on the Linux command line results in quite a bit more data than just the date. It also includes the day of the week, the current time and the time zone.

$ date
Mon Oct 16 11:24:44 AM EDT 2023

The command shown below gives displays the date in the shorthand month/day/year format.

$ date +%D
10/16/23

Seeing date information for any date

In fact, the date -d command will provide information on any date you specify. If you need to know the day of the week that Nov 11th will fall on in some particular year, enter a command like this one:

$ date -d 11/11/23
Sat Nov 11 12:00:00 AM EST 2023

Aha, it’s a Saturday — maybe a good day for you to visit some friends!

If you want to see the day-of-week information for a bunch of birthdays with only a single command, you can take a list of important birthdays stored in a file like this:

$ cat birthdays
Jan 4, 1972
Mar 18, 1949
May 1, 1976
Apr 1, 2017
Jan 8, 1954
Sep 23, 1979
Aug 6, 1956
May 2, 2014

Then run the date command using the -f option like this:

$ date -f birthdays
Tue Jan  4 12:00:00 AM EST 1972
Fri Mar 18 12:00:00 AM EST 1949
Sat May  1 12:00:00 AM EDT 1976
Sat Apr  1 12:00:00 AM EDT 2017
Fri Jan  8 12:00:00 AM EST 1954
Sun Sep 23 12:00:00 AM EDT 1979
Mon Aug  6 12:00:00 AM EDT 1956
Fri May  2 12:00:00 AM EDT 2014

To see the current date and time in the RFC-2822 format, use a command like this one:

$ date --rfc-2822
Mon, 16 Oct 2023 12:06:00 -0400

You can also use this shortened method:

$ date -R
Mon, 16 Oct 2023 12:06:02 -0400

Seeing dates associated with files

You can even use the date command to display the last update time for a file using the date -r command:

$ date -r notes
Tue Sep 19 01:17:37 PM EDT 2023

Using time zones

To get the current date and time for a particular time zone, use a command like this one:

$ TZ=America/New_York date
Mon Oct 16 12:17:51 PM EDT 2023

Be careful to spell the time zone correctly or you might get a response that makes you scratch your head and wonder what the answer really means:

$ TZ="America/Spaghetti" date
Mon Oct 16 05:43:43 PM America 2023

Hmm, Spaghetti must be a very interesting time zone! Let’s check on an old favorite of mine.

$ TZ="America/Twilight_Zone" date
Mon Oct 16 05:43:46 PM America 2023

OK, why not?

Viewing time/date details

Undoubtedly the most detailed time/date command available on Linux is the timedatectl command that provides the time in both local and UTC formats along with some additional information on your system settings.

$ timedatectl
               Local time: Mon 2023-10-16 11:01:40 EDT
           Universal time: Mon 2023-10-16 15:01:40 UTC
                 RTC time: Mon 2023-16 15:01:40
                Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

NTP in the next-to-last line in the output refers to the “network time protocol” — the internet protocol used to synchronize computer clock time sources in a network and part of TCP/IP.

Viewing time zones

Linux recognizes nearly 600 time zones that the date command can use. To list them all, try the command timedatectl list-timezones. Here’s a shortened example of what you will see:

$ timedatectl list-timezones | grep America | column | head -5
America/Adak                            America/Jamaica
America/Anchorage                       America/Jujuy
America/Anguilla                        America/Juneau
America/Antigua                         America/Kentucky/Louisville
America/Araguaina                       America/Kentucky/Monticello

WARNING: You won’t very likely find “Spaghetti” or the “Twilight Zone”.

$ timedatectl list-timezones | grep Spaghetti
$

You can also ask the date command to supply a single piece of information on dates. For example, you can ask for the day of the week to be spelled out fully like this:

$ date +%A
Monday

Additional single value options include:

Option  Provides                                       Example
%B      The full month name	                       Monday
%F      The date in YYYY-MM-DD format                  2023-10-16
%H      The hour in 24-hour format                     21
%I      The hour in 12-hour format                     9
%j      The day of the year                            289
%S      Seconds                                        34
%V      The week of the year                           35
%x      The date representation based on the locale    10/16/2023
%X      The time representation based on the locale    09:09:11 PM
%Y      Year                                           2023

Wrap-up

The date command can display the date and time in probably any way you might want – and then some!

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author