Categories: Time and Location

Linux date command (date and time) [Basic Guide]

The date command in Linux can be used to show the date or set the system date.

The most common options are:

  • -d string Show the times in a certain format
  • -u Show or configure the times in the format UTC (Coordinated Universal Time)
  • -s & nbsp; Configure the hours

Coordinated Universal Time, abbreviated UTC (from English Coordinated Universal Time), also known as civil time, is the reference time zone from which all other time zones in the world are calculated.

See the examples:

$ date Sat Oct 19, 2019 1:13:02 PM -03

The “-u” option shows the times in UTC format:

$ date -u Sat Oct 19, 2019 4:13:45 PM UTC

The date command allows you to display a date in several formats:

%D: mm/dd/yy.
%d: Day of the month (01 to 31).
%a: Abbreviated days of the week (Sun, Mon… Sat).
%A: Days of the week in full (Sunday, Monday… Saturday).
%h or %b: Abbreviated month (Jan, Feb… Dec).
%B: Full month (January… December).
%m: Month in numerals (01 to 12).
%y: The last two digits of the year (00 to 99).
%Y: Four-digit year (2019).
%T: Time in the 24-hour format HH:MM:SS.
%H: Time.
%M: Minute.
%S: Second.
%j: Day of the year (122)
%u: day of the week (1… 7) – 7 represents Sunday.
%w: day of the week (0… 6) – 0 represents Sunday.

Examples:

$ date +'Today is %A, %d of %B of %Y, the %j day of the year, %H: %M' Today is Saturday, November 02, 2019, the 292nd day of the year, at 14:14

This example was strange because it shows the date with location in English with a phrase in Portuguese.

To change the location and display everything in Portuguese, we can change the LC_ALL variable:

$ export LC_ALL=en_BR.UTF-8

When you run the date command again, the date will be in Portuguese:

$ date +'Today is %A, %d of %B of %Y, the %j day of the year, %H: %M' Today is Saturday, November 02, 2019, the 292nd day of the year, at 14:15

Changing the date format can be useful to be used in a variable to be used in a script:

#!/bin/bash
echo "Data backup"
export DATE=$(date +'%F-%H%M%S')
tar cvzf /home/uira/backup-$DATE.tar.gz /etc

The script above writes the date in the format YYYY-MM-DD-HHMMSS in the DATE variable. It then uses the variable as part of the name of a tar file that backs up the /etc. directory. The result of the execution will be a backup-2019-10-19-142339.tar.gz file.

Note that the result of a command can be used in a variable if it is contained in $ (command).

Change the Date on Linux

The date command can also be used to change the date of the system clock. In this example, I changed the system date to my birthday:

# date +%y%m%d -s “2019-05-11" 20190511
$ date Sat May 11 00:00:01 -03 2019

The TZ variable changes the date and time shown by the date command to the time zone.

Did you like it?

Share

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Share
Published by
Uirá Endy Ribeiro

Recent Posts

Sudo command on Linux (privilege scale) [Basic Guide]

The sudo command on Linux executes a given command as if it were another user.…

2 years ago

SS command on Linux (investigate the network) [Basic Guide]

The ss command on Linux is extremely useful for investigating sockets, providing various information about…

2 years ago

Free Linux command (memory usage) [Basic Guide]

Free Linux command shows the amount of total memory in use and available, as well…

2 years ago

Linux while command (loop – while) [Basic Guide]

The shell has structures for testing conditions and executing certain program sequences several times (loop),…

2 years ago

Linux fstab file (disk mount setup) [Basic Guide]

The /etc/fstab file stores the configuration of which devices should be mounted and what is…

2 years ago

Netcat command on Linux (Swiss network knife) [Basic Guide]

The Netcat Command in Linux or nc is a utility used to do “almost anything”…

2 years ago

This website uses cookies.