The date command is part of the GNU Core Utilities and is used to display and set the time of your system. Time is very important to all functions of an operating system. Scheduled tasks, file attributes, and even software and access authorization, all rely on time to function correctly. In this article will look into the date command, it's functionalities and how to use it in a Linux Environment.

Date Command: What is it?

The Date Command is a utility that displays, sets or changes your system's date and time. The default date and time displayed depends on the time zone in which your operating system was set up (or the one defined in "/etc/localtime"). The file "/usr/share/zoneinfo" holds a list of configurable timezones. In order to make modifications to the system time, however, you need to be the root user or have sudo elevated privileges.

How the Date Command Works

The Real Time Clock (RTC) is a piece of hardware (or circuitry) located on your Motherboard. The circuit is battery-operated and it keeps the time even when you turn off your machine. When you turn on your machine, the kernel extracts the time from your clock and sets the system time. Think of this as implying that your computer has a software clock (system time) and a hardware clock. These run separately and often synchronized with each other.

The date command displays or changes the software clock also called system time. It does not interface with the hardware clock. Other utilities exists for the express purpose of changing, or syncing the hardware clock.

Unix / Epoch Time

In Linux and UNIX, time is often represented in epoch or Unix time. This is especially true for timestamps.

Epoch - An event or a time marked by an event that begins a new period or development.

Merriam-Webster

Epoch Time is the time, in seconds, since midnight (00:00:00, UTC) of Thursday, January 1 1970. Interestingly, even though epoch time is based on the UTC timezone, time in Unix Time (epoach) is not in itself specific to time zones. Servers in different time zones both record and refer to the same point in time when they refer to a specific Unix time. When this Unix time is converted to human-readable date and time stamps, the time will then be specific to the timezone. This presents the January 2038 problem, which in itself, is an interesting read. See the resources section below for more information on epoch and the January 2038 problem.

Using the Date Command

The base form of the date command follows the syntax below:

date [option]… [format]

Without any options or formats specified, the date command returns the following:

[cherisim@putor ~]$ date
 Mon 14 Oct 2019 11:29:24 AM EDT

Interpreting the output, from left to right:

Mon - Day of the week abbreviated.
14 - Day of the month
Oct - Month abbreviated
2019 - Year
11:29:24 - Hour, Minute, Seconds
AM - The period (AM / PM)
EDT - Time Zone

Let us now try to apply a few options and show examples.

Display time in UTC or GMT

The -u (--utc) displays or prints the time in Coordinated Universal Time or Greenwich Mean Time.

[cherisim@putor ~]$ date -u
 Mon 14 Oct 2019 03:37:27 PM UTC

Did you know that there is a difference between GMT and UTC even though they share the same current time? In short, GMT is an actual time zone, whereas UTC is a time standard that is used to keep time synchronized across the world.

-Word Time Server

Describe Time in Human Friendly Format

Using the -d (--date="STRING") option allows you to display time that is described by "STRING". This is often handy for more a more human friendly way to describe time. For example, you wouldn't say to your friend "Remember that Dave Matthews concert on October 14th 2017?". It would be more natural to say "Remember that Dave Matthews concert 2 years ago?".

A few examples:

Time two years ago from this moment:

[cherisim@putor ~]$ date -d "2 years ago"
 Sat 14 Oct 2017 11:45:16 AM EDT

Five days ago from this moment:

[cherisim@putor ~]$ date -d "5 days ago"
 Wed 09 Oct 2019 11:48:21 AM EDT

35 seconds ago:

[cherisim@putor ~]$ date -d "35 seconds ago"
 Mon 14 Oct 2019 11:48:47 AM EDT

Yesterday:

[cherisim@putor ~]$ date -d "yesterday"
 Sun 13 Oct 2019 11:50:31 AM EDT

Display time from a human friendly calendar representation:

[cherisim@putor ~]$ date -d "8/16/2003"
 Sat 16 Aug 2003 12:00:00 AM EDT

Using the Date Command to Set the System Date and Time

So far, none of our examples go beyond displaying the date, leaving the actual date and time unchanged. If we want to change the system date and time, we use the option --set or -s. The syntax for this command and associated option is as follows:

[cherisim@putor ~]$ date --set="desired date"

NOTE: You must be root or have elevated privileges to change the system date.

Applying this, we would have:

[cherisim@putor ~]$ sudo date --set="Sat Oct 14 13:23:53 EDT 1989"
 Sat 14 Oct 1989 01:23:53 PM EDT
[cherisim@putor ~]$ date
 Sat 14 Oct 1989 01:23:55 PM EDT

NOTE: Do NOT set your clock back to 1989, this is for demonstration purposes only.

If you set your clock back to 1989 you will most likely have some issues. For example, most web pages use SSL certificates that have an issues on and valid until date. I doubt any were issued in 1989.

Using the Date Command: Formatting the Output

Format specifiers come at the end of your date command and they tell the system how to present the results of the system call. Perhaps you would like to see the date in the common mm/dd/yy format or perhaps you want the weekdays named in full. Your Linux terminal can be tactfully used to such effect. The syntax for using the format specifiers is as follows:

date +%[format specifier]

NOTE: The plus sign in the above example tells date that formatting strings follow.

Below is a list of format specifiers commonly used with the date command:

  • %D - shows the date in the mm/dd/yy format
  • %d - shows what day of the month it is (from 01 to 31)
  • %A - shows the weekdays named in full (Monday, Tuesday, Wednesday, etc)
  • %a - shows the weekday names in abbreviated form
  • %h - shows the month names in abbreviated form
  • %B - shows the month name printed in full
  • %b - shows the month names in abbreviated form
  • %m - shows what month of the year it is (from 01 to 12)
  • %Y - shows what year it is in 4 digits
  • %y - shows the last 2 digits of what year it is (from 0 to 99)
  • %T - shows the 24-hour time in the HH:MM:SS format
  • %H - shows the hour only
  • %M - shows the minute
  • %S - shows the seconds

Interestingly, when you punch in the following command:

[cherisim@putor ~]$ date +"%s"

Your output looks something like this:

1571056096

If you remember the earlier explanation, this is the time, in seconds, from epoch! Try it out!

More Examples of Formatting the Date Command

Tradition Unites States date:

[cherisim@putor ~]$ date +%m/%d/%Y
 10/20/2019

NOTE: We can use characters in between the spacing. Here we used the backslash as traditionally done in the United States.

U.S. Military date format:

[cherisim@putor ~]$ date "+%d %h %Y"
 20 Oct 2019

NOTE: You must wrap the formatting in quotes if using spaces.

As always, the commands, options and format lists contained herein are by no means exhaustive. Linux's rich library of documentation provides a wealth of further materials that can be used to fine tune the displayed result.

Option --help: predictably, this shows a list of formatting options associable with the date command in your Linux distribution.

Conclusion

The date command is a great utility for easily setting your system time. Because of it's myriad of formatting options, it is also great to scripting and quick command line pipes. You should now feel comfortable using the date command and hopefully we learned some other interesting facts about epoch and the year 2038 problem.