Bash shell on Unity desktop concept
Fatmawati Achmad Zaenuri/Shutterstock.com

The date command is found in the Bash shell, which is the default shell in most Linux distributions and even macOS. This tutorial shows you how to master date on the command line and how you can use it in shell scripts to do more than simply print the time.

Run the date command to see this information. It prints the current date and time for your timezone:

date

Output of the date command

The default formatting looks a little goofy. Why isn’t the year printed after the month and day, instead of being tagged on at the end, behind the timezone? Have no fear: If it’s control over the format of the output you want, date delivers it in spades. There are more than 40 options you can pass to date to instruct it to format its output precisely as you’d like.

To use any of the options type date, a space, a plus sign +, and the option including the leading percentage sign. The %c (data and time in locale format) option causes the date and time to be printed in the normalized format associated with your locale. Your locale is set by the geographical and cultural information you provided when you installed your operating system. The locale governs such things as the currency symbol, paper sizes, timezone, and other cultural norms.

date +%c

Output of the date command with c option

The year now appears in a more natural position in the output.

You can pass several options to date at once. A sequence of options is called a format string. To see the name of the day (%A), the day of the month (%d) and the month name (%B), use this command:

date +%A%d%B

Output of the date command with A d B options

That worked, but it is ugly. No problem, we can include spaces as long as we wrap the entire format string in quotation marks. Note that the + goes outside the quotation marks.

date +"%A %d %B"

Output of the date command with A d B option with spaces

You can add text to the format string, like this:

date +"Today is: %A %d %B"

Output of the data command with user added text

Scrolling up and down through the date man page looking for the option you want soon becomes tiresome. We’ve wrangled the options into groups to help you find your way around them more easily.

Options to Display the Date and Time

  • %c: Prints the date and time in the format for your locale, including the timezone.

Output of the date command

Options to Display the Date

  • %D: Prints the date in mm/dd/yy format.
  • %F: Prints the date in yyyy-mm-dd format.
  • %x: Prints the date in the format for your locale.

Output of the date command with D F x options

Options to Display the Day

  • %a: Prints the name of the day, abbreviated to Mon, Tue, Wed, etc.
  • %A: Prints the full name of the day, Monday Tuesday, Wednesday, etc.
  • %u: Prints the number of the day of the week, where Monday=1, Tuesday=2, Wednesday=3, etc.
  • %w: Prints the number of the day of the week, where Sunday=0, Monday=1, Tuesday=2, etc.
  • %d: Prints the day of the month, with a leading zero (01, 02 … 09) if required.
  • %e: Prints the day of the month, with a leading space (‘ 1’, ‘ 2’ … ‘ 9’) if required. Note the apostrophes do not print.
  • %j: Prints the day of the year, with up to two leading zeroes, if required.

Output of the date command with a A u w d e j options

Options to Display the Week

  • %U: Prints the week number of year, considering Sunday as the first day of the week. For example, the third week of the year, twentieth week of the year, etc.
  • %V: Prints the ISO week number of the year, considering Monday as the first day of the week.
  • %W: Week number of the year, considering Monday as the first day of the week.

Output of the date command with U V W options

Options to Display the Month

  • %b or %h: Prints the name of the month abbreviated to Jan, Feb, Mar, etc.
  • %B: prints the full name of the month, January, February, March, etc.
  • %m: Prints the number of the month, with a leading zero if required 01, 02, 03 … 12.

Output of the date command with b h B m options

Options to Display the Year

  • %C: Prints the century without the year. In 2019 it would print 20.
  • %y: Prints the year as two digits. in 2019 it will print 19.
  • %Y: Prints the year as four digits.

Output of the date command with C y Y options

Options to Display the Time

  • %T: Prints the time as HH:MM:SS.
  • %R: Prints the hour and minutes as HH:MM with no seconds, using the 24-hour clock.
  • %r: Prints the time according to your locale, using the 12-hour clock and an am or pm indicator.
  • %X: Prints the time according to your locale, using the 24-hour clock. Allegedly. Note that during testing this option behaved exactly as %r does, as shown below. On a Linux machine configured for the UK locale and set to GMT, it printed the time, using the 24-hour clock with no AM or PM indicator, as expected.

Output of the date command with T R r X options

Options to Display the Hour

  • %H: Prints the hour 00, 01, 02…23.
  • %I: Prints the hour using the 12-hour clock, 00, 01, 02 … 12, with a leading zero if required.

Output of the date command with H I options

Options to Display Minutes

  • %M: prints the minute, 01, 02, 03  … 59, with a leading zero if required.

Output of the date command with M options

Options to Display Seconds

  • %s: Prints the number of seconds since 1970-01-01 00:00:00, the start of the Unix Epoch.
  • %S: Prints the seconds, 01, 02, 03 … 59, with a leading zero if required.
  • %N: Prints the Nanoseconds.

Output of the date command with s S N options

Options to Display Timezone Information

  • %z: Prints the time difference between your timezone and UTC.
  • %:z: Prints the time difference between your timezone and UTC, with a : between the hours and minutes. Note the : between the % sign and z .
  • %::z: Prints the time difference between your timezone and UTC, with a : between the hours, minutes and seconds. Note the :: between the % sign and z .
  • %Z: Prints the alphabetic timezone name.

Output of the date command with timezone options

Options Related to Formatting

  • %p: Prints the AM or PM indicator in uppercase.
  • %P: Prints the am or pm indicator in lowercase. Note the quirk with these two options. A lowercase p gives uppercase output, an uppercase P gives lowercase output.
  • %t: Prints a tab.
  • %n: Prints a new line.

Output of the date command with AM PM indicator and formatting options

Options to Modify Other Options

These modifiers can be inserted between the % and the option letter of other options to modify their display. For example, %-S would remove the leading zero for single-digit seconds values.

  • : A single hyphen prevents zero padding on single digit values.
  • _: a single underscore adds leading spaces for single digit values.
  • 0: Provides leading zeroes for single digit values.
  • ^: Uses uppercase, if possible (not all options respect this modifier).
  • #: Use the opposite to the default case for the option, if possible (not all options respect this modifier).

Output of the date command with formatting options

Two More Neat Tricks

To get the last modification time of a file, use the -r (reference) option. Note that this uses a - (hyphen) instead of a % sign, and it doesn’t require a + sign. Try this command in your home folder:

date -r .bashrc

Output of the date command with file modification time option

The TZ setting allows you to change your timezone for the duration of a single command.

TZ=GMT date +%c

Output of the date command for a different timezone

Using Date in Scripts

Enabling a Bash shell script to print the time and date is trivial. Create a text file with the following content, and save it as gd.sh.

#!/bin/bash

TODAY=$(date +"Today is %A, %d of %B")
TIMENOW=$(date +"The local time is %r")
TIME_UK=$(TZ=BST date +"The time in the UK is %r")

echo $TODAY
echo $TIMENOW
echo $TIME_UK

Type the following command to set the execution permissions and make the script executable.

chmod +x gd.sh

Run the script with this command:

./gd.sh

Output of the gd.sh script

We can use the date command to provide a timestamp. The script shown will create a directory with the timestamp as its name. It will then copy all text files from the current folder into it. By running this script periodically we can take a snapshot of our text files. Over time we’ll build up a series of folders with different versions of our text files in them.

Note that this isn’t a robust backup system, it’s just for illustrative purposes.

Create a text file with the following content, and save it as snapshot.sh.

#!/bin/bash

# obtain the date and time
date_stamp=$(date +"%F-%H-%M-%S")

# make a directory with that name
mkdir "$date_stamp"

# copy the files from the current folder into it
cp *.txt "$date_stamp"

# all done, report back and exit
echo "Text files copied to directory: "$date_stamp

Type the following command to set the execution permissions and make the script executable.

chmod +x snapshot.sh

Run the script with this command:

./snapshot.sh

Effect of running the snapshot.sh script

You’ll see that a directory has been created. Its name is the date and time at which the script was executed. Inside that directory are copies of the text files.

Given a bit of thought and creativity, even the humble date command can be put to productive use.

Dave McKay Dave McKay
Dave McKay first used computers when punched paper tape was in vogue, and he has been programming ever since. He has worked as a freelance programmer, manager of an international software development team, an IT services project manager and is now a Data Protection Officer. He is a Linux evangelist and open source advocate.
Read Full Bio »