Americas

  • United States
sandra_henrystocker
Unix Dweeb

Displaying dates and times your way in Linux

How-To
Nov 26, 20195 mins
Linux

The Linux date command provides more options for displaying dates and times than you can shake a stick at (without hurting your wrist anyway). Here are some of the more useful choices.

A silhouette composed of a series of clocks  >  time / transition / impermanence
Credit: Thinkstock / Tomislav Jakupec

The date command on Linux systems is very straightforward. You type “date” and the date and time are displayed in a useful way. It includes the day-of-the-week, calendar date, time and time zone:

$ date
Tue 26 Nov 2019 11:45:11 AM EST

As long as your system is configured properly, you’ll see the date and current time along with your time zone.

The command, however, also offers a lot of options to display date and time information differently. For example, if you want to display dates in the most useful format for sorting, you might want to use a command like this:

$ date "+%Y-%m-%d"
2019-11-26

In this case, the year, month and day are arranged in that order. Note that we use a capital Y to get a four-digit year. If we use a lowercase y, we’d see only a two-digit year (e.g., 19). Don’t let this induce you into thinking that if %m gives you a numeric month, %M might give you the name of the month. No, %M will report on minutes. To get the month in abbreviated name format, you would use %b and for a fully spelled out month, you would use %B.

$ date "+%b %B"
Nov November

Alternately, you might want to display the date in this commonly used format:

$ date +%D
11/26/19

If you need a four-digit year, you can do this:

$ date "+%x"
11/26/2019

Here’s an example that might be useful. Say that you need to create a daily report and have the file name include the date, you could use a command like this to create the file – probably in a script:

touch Report-`date "+%Y-%m-%d"`

When you list your reports, they’ll list in date order or reverse date order if you add -r.

$ ls -r Report*
Report-2019-11-26
Report-2019-11-25
Report-2019-11-22
Report-2019-11-21
Report-2019-11-20

You can add other details to your date strings as well. The variety of options available is surprising. You could show which quarter of the year you’re in by using date “+%q” or display the date it was two months ago with a command like this:

$ date --date="2 months ago"
Thu 26 Sep 2019 09:02:43 AM EDT

Want to see what next Thursday’s date will be? You can use a command like date –date=”next thu”, but understand that, for Linux, next Thursday means whatever Thursday follows today. That’s tomorrow if today is Wednesday – not Thursday of next week. However, you can specify Thursday of next week as in the second command below.

$ date --date="next thu"
Thu 28 Nov 2019 12:00:00 AM EST
$ date --date="next week thu"
Thu 05 Dec 2019 12:00:00 AM EST

The man page for the date command lists all of its options. The list is fairly mind boggling, but you’ll probably find some date/time display options that work really well for you. Here are some that you might find interesting.

The date in universal time (UTC):

$ date -u
Tue 26 Nov 2019 01:13:59 PM UTC

The number of seconds since Jan 1, 1970 (related to how dates are stored on Linux systems):

$ date +%s
1574774137

Here’s a full listing of the date command’s options. As I said, it’s a lot more extensive than most of us likely imagine.

%%  a literal %
%a  locale's abbreviated weekday name (e.g., Sun)
%A  locale's full weekday name (e.g., Sunday)
%b  locale's abbreviated month name (e.g., Jan)
%B  locale's full month name (e.g., January)
%c  locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
%C  century; like %Y, except omit last two digits (e.g., 20)
%d  day of month (e.g., 01)
%D  date; same as %m/%d/%y
%e  day of month, space padded; same as %_d
%F  full date; same as %Y-%m-%d
%g  last two digits of year of ISO week number (see %G)
%G  year of ISO week number (see %V); normally useful only with %V
%h  same as %b
%H  hour (00..23)
%I  hour (01..12)
%j  day of year (001..366)
%k  hour, space padded ( 0..23); same as %_H
%l  hour, space padded ( 1..12); same as %_I
%m  month (01..12)
%M  minute (00..59)
%n  a newline
%N  nanoseconds (000000000..999999999)
%p  locale's equivalent of either AM or PM; blank if not known
%P  like %p, but lower case
%q  quarter of year (1..4)
%r  locale's 12-hour clock time (e.g., 11:11:04 PM)
%R  24-hour hour and minute; same as %H:%M
%s  seconds since 1970-01-01 00:00:00 UTC
%S  second (00..60)
%t  a tab
%T  time; same as %H:%M:%S
%u  day of week (1..7); 1 is Monday
%U  week number of year, with Sunday as first day of week (00..53)
%V  ISO week number, with Monday as first day of week (01..53)
%w  day of week (0..6); 0 is Sunday
%W  week number of year, with Monday as first day of week (00..53)
%x  locale's date representation (e.g., 12/31/99)
%X  locale's time representation (e.g., 23:13:48)
%y  last two digits of year (00..99)
%Y  year
%z  +hhmm numeric time zone (e.g., -0400)
%:z  +hh:mm numeric time zone (e.g., -04:00)
%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z  numeric  time  zone  with  :  to necessary precision (e.g., -04, +05:30)
%Z  alphabetic time zone abbreviation (e.g., EDT)
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.