Linux date command (date and time)
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:
The “-u” option shows the times in UTC format:
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:
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:
When you run the date command again, the date will be in Portuguese:
Changing the date format can be useful to be used in a variable to be used in a script:
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:
The TZ variable changes the date and time shown by the date command to the time zone.
Did you like it?
Share