Meet the Linux date command. No, it can't get you a romantic evening. But it can format the date at the top of a love letter you wrote in the terminal. Close enough? Let's get started.

As you're scripting in Bash, you'll inevitably need to print a date or time, and that date or time will often need to be in a specific format to satisfy the requirements of other functions. That's when the date command comes into play.

As you'll see, the date command in Linux is both simple and versatile, meaning it will accept all kinds of input and generate dates in a multitude of formats. It also has other special functions for various time-related computing tasks. Learning date's options and syntax will definitely make you more skilled in scripting, and maybe more punctual.

Date Command Basic Syntax

The basic syntax for the date command is as follows:

        date [OPTION]... [+FORMAT]
    

That means that after entering date, you can enter an option, like -d or -s, to invoke a particular function, which we'll explain below.

You can also follow those up with formatting strings, which always begin with a + character. Those strings take specific formatting characters, also listed below, to define the output.

Linux date Command Practical Examples

You can put the date command to use in several ways. Let's consider the most common and useful use cases of the same.

1. Get the Current Date and Time

You can get the current local date and time in the default format by passing the date command on its own.

        $ date
Mon 19 Apr 2021 12:41:17 PM CDT

As you can see, date gives you the relevant date and time information in a simple and predictable format.

2. Get a Past or Future Date

Let's say in your script you need to calculate the time and date that's exactly one week from now. The date command has got you covered. Issue this command, utilizing the -d option to get detailed information related to future dates:

        $ date -d "next week"
Tue 27 Apr 2021 05:21:07 PM CDT

The -d option, short for date, is where the date command really shines. It will accept a variety of custom date strings; they can be technical, like 20200315, 03/15/20, or readable like Mar 15 2020. But you can also use relative terms, like tomorrow, yesterday, next Sunday, and more. Play around with it and see how date interprets different input strings.

3. Format a Date

You might have noticed in the previous two examples, date defaults to a very specific time format. So what if you need it in a different format?

You can format your output similar to the printf command. For example, you can print the current year with this command:

        date +"Year: %Y"
    

The + signals that you want a formatted string, and whatever appears afterward inside of the quote marks, date will process and format for the output.

Here's a list of the formatting characters you're most likely to use:

Formatting Character

Output

%H

Hour (00-24)

%I

Hour (01-12)

%M

Minute (00-59)

%S

Second (00-60)

%p

AM or PM

%A

Weekday full name (e.g. Sunday)

%a

Weekday abbreviated name (e.g. Sun)

%w

Weekday number (0-6)

%d

Day of the month (01-31)

%j

Day of the year (001-366)

%B

Month full name (e.g. January)

%b

Month abbreviated name (e.g. Jan)

%m

Month number (01-12)

You can get a full list of formatting characters using the --help option in the terminal.

        date --help
    

4. Get the Day of the Week

One very common and practical use of date formatting is getting the day of the week for any given date. For example, to check what day of the week November 4, 1995 fell on, enter a command similar to this:

        $ date -d "1996-04-11" +"%A"
Friday

The -d option indicates that you want a specific date, the "1996-04-11" string indicates which date you want, and the +"%A" formatting indicates that you want the day of the week in the output. Remember that the date string can be in many formats, not just the one specified here.

5. Get Coordinated Universal Time

By issuing the -u flag, you can get the current time in Coordinated Universal Time (UTC).

        $ date -u
Wed 21 Apr 2021 12:46:59 PM UTC

6. Output the Local Time in Another Time Zone

If you need to get a date in any other time zone, you can do so by setting the TZ= environment variable before the date command.

For example, you can see the current date and time in Mountain Standard Time (MST) with the following command:

        $ TZ=MST date
Tue 20 Apr 2021 03:45:29 PM MST

For your purposes, simply replace MST with the initials for whatever time zone you prefer. You can also opt to use UTC notation. For example, to get the same time zone, replace MST with UTC+7.

In addition, you can name a continent and major city to get the time that particular city's local time. For example:

        $ TZ=America/Phoenix date
Tue 20 Apr 2021 03:45:29 PM MST

7. Get a File's Last Modification Time

If you're creating backups, for example, you'll often need to get the last modification date of a file. You can do this by passing the -r option and naming a file.

        $ date -r /etc/shadow
Wed 14 Apr 2021 07:53:02 AM CDT

You can change the timestamps of a file using the touch command in Linux as well.

8. Output and Convert Epoch Time

You can calculate the number of seconds since the Unix epoch with the following command:

        $ date +%s
1618955631

You can also reverse the process and convert Unix time into a human-readable format through the use of the -d option and @ character.

        $ date -d @1618955631
Tue 20 Apr 2021 04:53:51 PM CDT

Calculating Unix time is useful if you need an exact second that will definitely keep other devices synchronized.

9. Temporarily Set the System Time

You can change your system clock from the terminal with the date command by passing the -s argument followed by the time you want. For example, you can set the system clock to 24 hours in the future with the following command:

        date -s "tomorrow"
    

Note that you'll need sudo privileges to pass this command. In addition, the change probably won't be persistent (meaning your clock will go back to the previous time after a reboot) because most distros use other utilities to manage your system clock that will override the change on boot.

Linux date Command Explained

As in life, you can't get away from time in Linux. That's why it's crucial you understand how to format and use it, through the date command. One thing you're sure to encounter in Linux file management is the various timestamps that files carry with them.