+5 votes
99 views
How to display the date and time in a Linux terminal?

in Guides by (551k points)
reopened | 99 views

1 Answer

+3 votes
Best answer

Set location on Linux
Options to display the date and time
Options for displaying the date
Options to display the day
Options to display the week
Options to display the month
Options to display the year
Options to display the time
Options to display the time
Options to display the minutes
Options to show seconds
Options for displaying time zone information
Format-related options
Options to modify other options
Two more ingenious tricks to make the process easier
Use of date in scripts and Linux
Not a good security system in Linux

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

Run the date command to see this information. Print the current date and time for your time zone:

  date  

The default format seems a bit silly but that's how Linux is sometimes. Why is the year not printed after the month and day, instead of being labeled last, behind the time zone? If it's control over the output format you want, the date delivers it in spades. There are over 40 options that you can go through to date to instruct you to format your output exactly how you would like it.

To use any of the options, enter the date, a space, a plus sign +, and the option that includes the leading percent sign. The% c option (data and time in locale format) causes the date and time to be printed in the normalized format associated with its location..

Set location on Linux

Your location is established by the geographic and cultural information that you provided when you installed the operating system. The locale governs such things as the currency symbol, paper sizes, time zone, and other cultural norms that are important to Linux.

  date +%c  

The year now appears in a more natural position in production..

You can go through multiple 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 name of the month (% B), use this command:

  date +%A%d%B  

That worked, but it's ugly. No problem, we can include spaces as long as we put the entire format string in quotes. Note that the + sign is outside the quotation marks.

  date +"%A %d %B"  

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

  date +"Hoy es: %A %d %B"  

Scrolling up and down through the date man page looking for the option you want soon becomes a bummer. We have grouped the options into groups to help you find your way more easily.

Options to display the date and time

date% c : Print the date and time in your local format, including the time zone.

Options for displaying the date

  • date% D : Prints the date in mm / dd / yyy format.
  • date% F : Prints the date in yyyy-mm-dd format.
  • date% x : Print the date in the format of your location.

Options to display the day

  • date% a : Prints the name of the day, abbreviated as Mon, Tue, Fri, etc.
  • date% A : Print the full name of the day, Monday, Tuesday, Wednesday, etc.
  • date% u : Prints the number of the day of the week, where Monday = 1, Tuesday = 2, Wednesday = 3, etc.
  • date% w : Prints the number of the day of the week, where Sunday = 0, Monday = 1, Tuesday = 2, etc.
  • date% d : Prints the day of the month, with a leading zero (01, 02… 09) if necessary.
  • date% e : Print the day of the month, with a blank space ('1', '2'… '9') if necessary.
  • date% j : Prints the day of the year, with up to two leading zeros, if necessary.

Note: apostrophes are not printed

Options to display the week

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

Options to display the month

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

Options to display the year

  • date% y : Print the year as two digits. In 2021 it will print 21.
  • date% Y : Print the year as four digits.

Options to display the time

  • date% T : Print the time as HH: MM: SS.
  • date% R : Print the hour and minute as HH: MM without seconds, using the 24 hour clock.
  • date% r : Prints the time according to your location, using the 12-hour clock and an am or pm indicator.
  • date% X : Print the time according to your location, using the 24-hour clock.

Note that during the test it behaved exactly the same as% r.

Options to display the time

Print the hour 00, 01, 02… .23.

date% I : Prints the time using the 12 hour clock, 00, 01, 02… 12, with a leading zero if necessary.

Options to display the minutes

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

Options to show seconds

  • date% s : Print the number of seconds since 1970-01-01 00: 00: 00: 00, the beginning of the Unix Epoch.
  • date% S : Prints the seconds, 01, 02, 03… 59, with a leading zero if necessary.

Options for displaying time zone information

  • date% z : Print the time difference between your time zone and UTC.
  • date%: z : Prints the time difference between the time zone and UTC, with a: between hours and minutes. Notice the: between the% sign and z.
  • date% :: z : Prints the time difference between the time zone and UTC, with a: between hours, minutes and seconds. Notice the :: sign between the% sign and z.
  • date% Z : Prints the alphabetical name of the time zone.

Format-related options

  • date% p : Print the AM or PM indicator in uppercase.
  • date% P : Prints the am or pm indicator in lower case. Notice the rarity with these two options. A lowercase p outputs uppercase, an uppercase P outputs lowercase.
  • date% t : Print a card.
  • date% n : Print a new line.

Options to modify other options

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

-: A simple dash prevents zero padding on single digit values.
_: An underscore adds spaces for single digit values.
0: Provides leading zeros for single digit values.
^: Use uppercase if possible (not all options respect this modifier).

Use the opposite of the default for the option, if possible (not all options honor this modifier).

Two more ingenious tricks to make the process easier

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

  date -r .bashrc  

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

Use of date in scripts and Linux

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 execute permissions and make the script executable.

  chmod +x gd.sh  

Run the script with this command:

  ./gd.sh  

We can use the date command to provide a timestamp. The script shown will create a directory with the timestamp as the name.

You will then copy all the 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 will create a series of folders with different versions of our text files in them.

Not a good security system in Linux

Please note that this is not a robust backup system, it is for illustration purposes only.

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 execute permissions and make the script executable.

  chmod +x snapshot.sh  

You will see that a directory has been created. Its name is the date and time the script was run. Inside that directory are copies of the text files.

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



by (3.5m points)

Related questions

+4 votes
1 answer
+5 votes
1 answer
+5 votes
1 answer
asked Feb 21, 2022 in Guides by backtothefuture (551k points) | 80 views
+4 votes
1 answer
+3 votes
1 answer
asked May 26, 2019 in Linux / Unix by backtothefuture (551k points) | 208 views
Sponsored articles cost $40 per post. You can contact us via Feedback
10,634 questions
10,766 answers
510 comments
3 users