Configuring the date and time RHEL 7 (2)

The date utility is available on all Linux systems and allows you to display and configure the current date and time. It is frequently used in scripts to display detailed information about the system clock in a custom format.

2.2.1. Displaying the Current Date and Time

To display the current date and time, run the date command with no additional command line options:
date
This displays the day of the week followed by the current date, local time, abbreviated time zone, and year.
By default, the date command displays the local time. To display the time in UTC, run the command with the --utc or -u command line option:
date --utc
You can also customize the format of the displayed information by providing the +"format" option on the command line:
date +"format"
Replace format with one or more supported control sequences

Table 2.1. Commonly Used Control Sequences

Control Sequence Description
%H The hour in the HH format (for example, 17).
%M The minute in the MM format (for example, 30).
%S The second in the SS format (for example, 24).
%d The day of the month in the DD format (for example, 16).
%m The month in the MM format (for example, 09).
%Y The year in the YYYY format (for example, 2013).
%Z The time zone abbreviation (for example, CEST).
%F The full date in the YYYY-MM-DD format (for example, 2013-09-16). This option is equal to %Y-%m-%d.
%T The full time in the HH:MM:SS format (for example, 17:30:24). This option is equal to %H:%M:%S

Example 2.6. Displaying the Current Date and Time

To display the current date and local time, type the following at a shell prompt:
~]$ date
Mon Sep 16 17:30:24 CEST 2013
To display the current date and time in UTC, type the following at a shell prompt:
~]$ date --utc
Mon Sep 16 15:30:34 UTC 2013
To customize the output of the date command, type:
~]$ date +"%Y-%m-%d %H:%M"
2013-09-16 17:30

2.2.2. Changing the Current Time

To change the current time, run the date command with the --set or -s option as root:
date --set HH:MM:SS
Replace HH with an hour, MM with a minute, and SS with a second, all typed in two-digit form.
By default, the date command sets the system clock to the local time. To set the system clock in UTC, run the command with the --utc or -u command line option:
date --set HH:MM:SS --utc

Example 2.7. Changing the Current Time

To change the current time to 11:26 p.m., run the following command as root:
~]# date --set 23:26:00

2.2.3. Changing the Current Date

To change the current date, run the date command with the --set or -s option as root:
date --set YYYY-MM-DD
Replace YYYY with a four-digit year, MM with a two-digit month, and DD with a two-digit day of the month.
Note that changing the date without specifying the current time results in setting the time to 00:00:00.

Example 2.8. Changing the Current Date

To change the current date to 2 June 2013 and keep the current time (11:26 p.m.), run the following command as root:
~]# date --set 2013-06-02 23:26:00