This post I will show you how to change the current date and time for your CentOS 7 Or RHEL 7 system using “date” command.
The “date” command can be used to display and set the current date and time .it is also used to display date or time information in a custom format in your automation scripts.
In the previous post, I wrote a guide to change the current date and time using “timedatectl” command. see also:
Changing the date and time using timedatectl command
CentOS 7 Displaying the Current date and time using “date” command
If you want to check the current date and time, just issue the “date” command in the command line interface:
date
Outputs:
[root@devops Desktop]# date Tue Dec 9 04:22:51 CET 2019
The above output will display the local time of you system, if you want to print the Universal Time(UTC), need to pass the “-u” or “–utc” option, such as:
date --utc
output:
[root@devops Desktop]# date --utc Tue Dec 9 03:29:03 UTC 2014
You can also use the FORMAT to controls the output of date command.
date +"<FORMAT>"
The FORMAT control sequence are below:
%% a literal % %a locale's abbreviated weekday name (e.g., Sun) %A locale's full weekday name (e.g., Sunday) %b locale's abbreviated month name (e.g., Jan) %B locale's full month name (e.g., January) %c locale's date and time (e.g., Thu Mar 3 23:05:25 2005) %C century; like %Y, except omit last two digits (e.g., 20) %d day of month (e.g., 01) %D date; same as %m/%d/%y %e day of month, space padded; same as %_d %F full date; same as %Y-%m-%d %g last two digits of year of ISO week number (see %G) %G year of ISO week number (see %V); normally useful only with %V %h same as %b %H hour (00..23) ...
Example: using “%F” sequence to control the output
[root@devops Desktop]# date +"%F" 2018-07-09
CentOS 7 Changing the Current Date
Issue the fllowing command to change the current date of your system:
date -s <YYYY-MM-DD>
Example: change the current date to “2018-10-28”
[root@devops Desktop]# date -s 2018-10-28 Tue Oct 28 00:00:00 CET 2018 [root@devops Desktop]# date Tue Oct 28 00:00:01 CET 2018
CentOS 7 Changing the Current Time
If you want to change the curret time, issue the following command:
date -s <HH:MM:SS>
Example:changing the current time to “13:23:45”
[root@devops Desktop]# date -s 13:23:45 Tue Dec 9 13:23:45 CET 2018 [root@devops Desktop]# date Tue Dec 9 13:23:46 CET 2018