date command
This command displays the operating system date and time. Administrators also use this command to set the date and time; therefore, only users with root privileges can do that. By default, the date command displays the date of the current time zone. Also, the date command can show dates and times in different formats and calculate future and past dates.
date Command Syntax
date [Options]……..[+FORMATS]
Option Examples
date command
“date” command with no options displays the time zone current date and time. It shows the day in the week, month, day of the month, the time using “:” as the delimiter, time zone, and the year respectively. In the example below, EDT stands for Eastern Daylight Time.
-u,–utc,–universal
This option with date command display or set the time in UTC format.
[root@Lpic1-CentOs9 ~]# date
Mon May 9 12:46:10 PM EDT 2022
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
[root@Lpic1-CentOs9 ~]# date -u
Mon May 9 05:03:31 PM UTC 2022
[root@Lpic1-CentOs9 ~]# date --utc
Mon May 9 05:03:36 PM UTC 2022
[root@Lpic1-CentOs9 ~]# date --universal
Mon May 9 05:03:53 PM UTC 2022
Note: in Linux, use a single hyphen for single-letter options and put double hyphens for using multi-letter options
-d,–date
Using date command with this option displays a time according to the string input so that users can operate on that specific date. Use the syntax below.
date –date=”string”
date -d “string”
Note: Put a multi-word string between two “so that Linux can understand the input string contains multiple words.
Using strings such as “ last month”, “ next monday”, and “ 5 sec ago” is possible in this command. Also, sending a specific date as the string input to this option displays the description of the date.
Note: To display the future date, write the input string in this format:
Six days from the current date and time: date –date=”6 day”
One year from the current date and time: date –date=”1 year”
10 sec from the current date and time: date –date=”10 sec”
[root@Lpic1-CentOs9 ~]# date --date=now
Mon May 9 02:05:10 PM EDT 2022
[root@Lpic1-CentOs9 ~]# date -d yesterday
Sun May 8 02:05:36 PM EDT 2022
[root@Lpic1-CentOs9 ~]# date -d "next tuesday"
Tue May 10 12:00:00 AM EDT 2022
[root@Lpic1-CentOs9 ~]# date -d now
Mon May 9 02:07:46 PM EDT 2022
[root@Lpic1-CentOs9 ~]# date --date="10 sec ago"
Mon May 9 02:07:37 PM EDT 2022
[root@Lpic1-CentOs9 ~]# date --date="next year"
Tue May 9 02:09:12 PM EDT 2023
[root@Lpic1-CentOs9 ~]#date
Tue May 10 10:19:05 AM +0430 2022
[root@Lpic1-CentOs9 ~]#date --date="10 sec"
Tue May 10 10:19:16 AM +0430 2022
**********Sending Specific Date*****************
************************************************
[root@Lpic1-CentOs9 ~]# date -d "2022-05-09 12:10:53"
Mon May 9 12:10:53 PM EDT 2022
[root@Lpic1-CentOs9 ~]# date -d "1987-04-20 08:00:53"
Mon Apr 20 08:00:53 AM EDT 1987
-r,–reference=FILE
Using this option displays the file’s last modification date and time with a file path. This kind of information is helpful in bash scripting. The example code below displays the last modification date of the “/etc/hosts” file.
[root@Lpic1-CentOs9 ~]#date -r /etc/hosts
Mon Dec 19 11:18:37 AM +0330 2022
[root@Lpic1-CentOs9 ~]#date --reference=/etc/hosts
Mon Dec 19 11:18:37 AM +0330 2022
-s,–set=String
To set system time and date, use the date command with the –set or –s option followed by an input string that describes the time. For Example, to set the system time and date to “11:52 am May 10, 2020” use the command as example code below.
Note: Most administrators use the ntp protocol to sync the system time and date with an ntp server. In this case, setting date and time manually causes serious problems.
[root@Lpic1-CentOs9 ~]#date --set="20220510 11:52"
Tue May 10 11:52:00 AM +0430 2022
date command Formating
Display the date command’s output in a specific format using Format options. Use —help option to see the list of available format options.
Format examples
Show only the date: date +%F
Show only the time: date +%T
Other format examples are available in the code below.
Note: Put a ‘+’ before the format string, otherwise the system displays an “invalid date” error, as in the example code below.
[root@Lpic1-CentOs9 ~]# date '%a %d-%m-%y'
date: invalid date ‘%a %d-%m-%y’
[root@Lpic1-CentOs9 ~]#
[root@Lpic1-CentOs9 ~]#date +%F
2022-05-10
[root@Lpic1-CentOs9 ~]#date +%T
14:54:03
[root@Lpic1-CentOs9 ~]#date +'%A %d-%m-%y'
Tuesday 10-05-22
[root@Lpic1-CentOs9 ~]#date +'%b %d-%m-%y'
May 10-05-22
[root@Lpic1-CentOs9 ~]#date +'%B %d-%m-%y'
May 10-05-22
[root@Lpic1-CentOs9 ~]#date +'%c %d-%m-%y'
Tue 10 May 2022 02:55:04 PM +0430 10-05-22
[root@Lpic1-CentOs9 ~]# date +'%C %d-%m-%y'
20 10-05-22
Practice
Run commands which display strings below :
A) Year: 2022 , Month: 05 , Day: 10
B)DATE: 05/10/22
TIME: 15:17:01
Week number: 19
Year: 22
Note: Use –help for all required format options
******************Answers***************************
********************A*******************************
[root@Lpic1-CentOs9 ~]#date +'Year: %Y , Month: %m , Day: %d'
Year: 2022 , Month: 05 , Day: 10
[root@Lpic1-CentOs9 ~]#date "+Year: %Y , Month: %m , Day: %d"
Year: 2022 , Month: 05 , Day: 10
#### Both Syntaxes are correct####
********************B******************************
[root@Lpic1-CentOs9 ~]#date "+DATE: %D%nTIME: %T%nWeek number: %V%nYear: %y"
DATE: 05/10/22
TIME: 15:16:26
Week number: 19
Year: 22
[root@Lpic1-CentOs9 ~]#date +'DATE: %D%nTIME: %T%nWeek number: %V%nYear: %y'
DATE: 05/10/22
TIME: 15:17:01
Week number: 19
Year: 22
#### Both Syntaxes are correct####