date command in Linux with examples

The date command in Linux is one of many fundamental commands used for primary and everyday purposes. The date command can be used to set and display the system time. Running this command as is (in its most basic state without arguments) will display the current time.

While the date command may seem incredibly simple based on our brief explanation above, it gets more complex depending on the options you use with it. These options allow the date command to execute a large variety of different operations related to date and time.

In this tutorial, you’ll learn how to use the date command in Linux through examples. Follow along below to learn about the various options that you can use with this command.

In this tutorial you will learn:

  • How to use the date command on Linux

"<yoastmark

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software date
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options

The date command in Linux will output the current date and time. Its many options are mostly just to format the output in different ways, as you’ll see in the examples below.

date command in Linux Basic Examples

  1. The date command displays and sets the time in your Linux terminal. Firstly, we’ll show below an example of how to use its syntax to display the time.
    $ date
    

    "Basic

  2. As you can see in the screenshot above, the output will display the time and the date with the time zone and day of the week included. But we can customize this specific formatting by utilizing certain characters after a plus sign + in the command.
    $ date +%m-%d-%Y
    

    As we’ve observed in the introduction of the date command above, the default dating format is as follows: “day of the week, day of the month, month, year, time, time zone.” The syntax above will instead give us an output that will display the time in the format MM-DD-YYYY.

    "Formatting

  3. Another common dating format to configure for the date command is YYYY-MM-DD. The syntax for this configuration is very similar to the one in the previous example. And like we did in that example, we’ll show it in a Linux terminal below.
    $ date +%f
    
    Date formatting using %F option
    Date formatting using %F option

    If you have a keen eye, you’ll notice that the syntax for this dating format was quite different from the previous one, even though the desired outputs only differ in the order of their placing.

    The syntax in the latter example contains the %f option instead of %d, %m, and %Y which are present in the former example. We use this option instead because it can serve to stand in specifically for the %Y-%m-%d format.

  4. Another useful way one can use the date command is to convert various units of time into dates. For example, the date command reads words such as “tomorrow” or “last Tuesday” as values which we can run through with the date command and -d option to convert into a date.
    $ date -d tomorrow
    

    Obtain tomorrows date
    Obtain tomorrows date
  5. It is also possible to identify the time and date of various things. The example below shows how to utilize the date command to display the time and date when a file was last modified.
    $ date -r /Downloads/file01
    

    Check for file modification date
    Check for file modification date
  6. In Linux, you can use the date command with a large selection of special options called format controls. We touched upon these for a brief moment in an earlier section of this tutorial.The few format controls that we explored were the %d, %m, %Y and the %f options. Here is a list of some other common format controls you can use with date.+%a – This option is the locale’s date and time.Locales are a collection of environment variables that define the location, time, date, and language. Programs read this set of variables to ascertain the information mentioned above that they carry.

    %+z – This option represents the time zone currently used on the Linux system.
    +%p – This option is what the system’s locale uses to represent AM and PM.

NOTE
You can always use the man command to read more about the date command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Advanced Usage

As we mentioned earlier, the date command can convert a specified unit of time into a date. We have already covered how to convert values of time like “tomorrow” into a date.

Next, we’ll show you how to convert some other time units in the coming examples below. One aspect of date that initially confuses most people is its use of the Unix epoch time.

An epoch denotes the number of seconds that have passed since the date January 1st, 1970. In case you’re wondering, there’s nothing special about this date. It’s merely an arbitrary date that was agreed upon some time ago and is used for convenience. We’ll show you how to use it below.

date command in Linux Advanced Examples

  1. The date command can display the number of seconds since the Unix epoch by using the %s option.
    $ date +%s
    

    "Using

  2. We can also convert that output into a date using the -d option, as well as an @ sign.
    $ date -d @1630029904
    

    Using the -d option with +%s to convert an epoch time
    Using the -d option with +%s to convert an epoch time

Closing Thoughts

The date command and a few of its options are useful to remember when you need to use timestamps, calculate time in a Bash script, or just check what time it is from your system’s terminal.