Show Table of Contents
3.2. Using the date Command
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.
For information on how to change the time zone or enable automatic synchronization of the system clock with a remote server, see Section 3.1, “Using the
timedatectl
Command”.
3.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 as illustrated in Example 3.6, “Displaying the Current Date and Time”. See Table 3.1, “Commonly Used Control Sequences” for a list of the most frequently used formatting options, or the
date
(1) manual page for a complete list of these options.
Table 3.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, 2016 ). |
%Z | The time zone abbreviation (for example, CEST ). |
%F | The full date in the YYYY-MM-DD format (for example, 2016-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 3.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 2016
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 2016
To customize the output of the
date
command, type:
~]$ date +"%Y-%m-%d %H:%M"
2016-09-16 17:30
3.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 3.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
3.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 3.8. Changing the Current Date
To change the current date to 2 June 2017 and keep the current time (11:26 p.m.), run the following command as
root
:
~]# date --set "2017-06-02 23:26:00"
Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.