How to display the date and time in the Linux terminal (and use it in bash scripts)

the date Command is in the bash shell, which is the default shell in most Linux distributions and even macOS. This tutorial will show you how to do it date on the command line and how you can use it in shell scripts to do more than just print the time.

Run the date Command to display this information. It prints the current date and time for your time zone:

date

The standard formatting looks a bit silly. Why isn’t the year printed by month and day instead of appending after the time zone? Don’t worry: if you want to control the output format you want, date delivers it in spades. There are 40+ options that you can pass on date to tell it to format the output exactly how you want it.

To use any of the options, enter date, a space, a plus sign +, and the option with the leading percent sign. the %c (Date and time in locale format) causes the date and time to be printed in the normalized format associated with your locale. Your locale is determined by the geographic and cultural information you provided when you installed your operating system. The locale governs things like the currency symbol, paper sizes, time zone, and other cultural norms.

date +%c

Issue the date command with option c

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


You can have several options. pass on date at once. A sequence of options is known as a format string. To display 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’s ugly. No problem, we can insert spaces as long as we enclose the entire format string in quotes. Note that the + goes outside the quotes.

date +"%A %d %B"

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

You can add text to the format string as follows:

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

Output of the data command with text added by the user

Scroll up and down through the date man page Finding the option you want quickly becomes tedious. We have grouped the options to make it easier for you to find your way around.

Options to view the date and time

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

Output of the date command

Options to display the date

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

Output of the date command with DF x options

Options for displaying the day

  • %a: Prints the name of the day, abbreviated as Mon, Tue, Wed, etc.
  • %A: Prints the full name of the day, Monday, Tuesday, Wednesday, etc.
  • % u: Prints the day of the week number, where Monday = 1, Tuesday = 2, Wednesday = 3, and so on.
  • % w: Prints the day of the week number, where Sunday = 0, Monday = 1, Tuesday = 2, and so on.
  • % D: Prints the day of the month, if necessary with a leading zero (01, 02… 09).
  • % e: Prints the day of the month with a leading space (‘1’, ‘2’… ‘9’), if required. Note that the apostrophes are not printed.
  • % J: Prints the day of the year with up to two leading zeros, if necessary.

Output of the date command with A uwdej options

Options to view the week

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

Issue the date command with UVW options

Options to view the month

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

Output of the date command with options bh B m

Options to view the year

  • % C: Prints the century without a year. In 2019 it would print 20.
  • %and: The year is printed with two digits. In 2019 it will be printed 19.
  • %AND: The year is printed in four digits.

Output of the date command with C y Y options

Time display options

  • % T: Prints the time as HH: MM: SS.
  • % R: Prints the hours and minutes in the HH: MM format without seconds in the 24-hour format.
  • % R: Prints the time according to your locale in 12-hour format and a morning or afternoon display.
  • % X: Prints the time in 24-hour format according to your locale. Allegedly. Note that this option works just like the %r does as shown below. On a Linux machine configured for the UK locale and set to GMT, the time was printed as expected in 24-hour format with no AM or PM display.

Output of the date command with TR r X options

Options to view the hour

  • %H: Prints the hours 00, 01, 02… 23.
  • %I: Prints the hour in 12-hour format, 00, 01, 02… 12, with a leading zero if necessary.

Output of the date command with HI options

Options to display minutes

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

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 era.
  • % S: Prints the seconds 01, 02, 03… 59, if necessary with a leading zero.
  • % N: Prints the nanoseconds.

Output of the date command with s SN options

Options for viewing time zone information

  • %With: Prints the time difference between your time zone and UTC.
  • %:With: Prints the time difference between your time zone and UTC with a: between the hours and minutes. Notice that : between % sign and z .
  • %::With: Prints the time difference between your time zone and UTC, with an: between the hours, minutes and seconds. Notice that :: between % sign and z .
  • %WITH: Prints the alphabetical time zone name.

Issue the date command with time zone options

Formatting options

  • % P: Prints the AM or PM indicator in uppercase.
  • % P: Prints the am or pm display in lower case. Note the quirk of these two options. One lower case letter p outputs capital letters, one capital letter P outputs lowercase letters.
  • % T: Prints a tab.
  • % n: Prints a new line.

Issue the date command with AM PM indicator and formatting options

Options to change other options

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

  • : A single hyphen prevents the padding of zeros in single-digit values.
  • _: A single underscore adds leading spaces for single-digit values.
  • 0: Provides leading zeros for single-digit values.
  • ^: Use capital letters whenever possible (not all options take this modifier into account).
  • #: Whenever possible, use the opposite of the default case for the option (not all options honor this modifier).

Output of the date command with formatting options

Two more nice tricks

To get the last time a file was modified, use the -r (Reference) option. Note that this a. used - (Hyphen) instead of a % sign, and it does not require a + Signature. Try this command in your home folder:

date -r .bashrc

Issue the date command with an option for the file modified time


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

TZ=GMT date +%c

Issue the date command for a different time zone

Use date in scripts

It is trivial to enable a bash shell script to print the time and date. 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

Enter the following command to set execute 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 creates a directory with the time stamp as the name. It then copies all text files from the current folder into it. By running this script regularly, we can take a snapshot of our text files. Over time, we will create a number of folders with different versions of our text files.

Note that this is not a robust backup system, 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


Enter the following command to set execute 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 will see that a directory has been created. Its name is the date and time the script ran. This directory contains copies of the text files.

With a little thought and creativity, even the humble date Command can be used productively.

Linux commands
Filestar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · Journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processesalias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networkingnetstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts