Date command

This command is used to display the date and time of the machine and to modify the date of the machine.

 

Syntax:

Date [option] [format]

 

The following are the options generally used with the date command.

-d  Displays the date and time based on the given string followed by the option.

-f   Displays the date and time based on the strings save on the file followed by the option.

-r   Displays the modification date and time of the file.

-s  to set the time based on the string followed by the option.

-u  To set the date and time in universal time format.

 

The following are the format controls that are used to display the output as required format.

%a  Prints abbreviated weekday name (e.g., Sun, mon etc.,)

%A  Printsfull weekday name (e.g., Sunday, Monday)

%b  Prints abbreviated month name (e.g., Jan)

%B  Prints full month name (e.g., January, February)

%d  Prints day of month (e.g, 01)

%D  Prints the date in the format of MM-DD-YY

%F  Prints the full date in the format of YYYY-MM-DD

%h  same as %b

%H  Prints hour (00..23)

%I   Prints hour (01..12)

%j   Prints day of year (001..366)

%m  Prints month (01..12)

%M  Prints minute (00..59)

%N  Prints nanoseconds (000000000..999999999)

%S  Displays second (00..60)

%S  Displays second (00..60)

%T  Prints time like HH:MM:SS in 24 hours format

%u  Prints day of week (1..7); 1 is Monday

%U  Prints week number of year, with Sunday as first day of week (00..53)

%Y  Prints year.

 

Example-1:

Print current date and time in the machine:

Simply use the date command to print the current date of the system.

[root@sys2 ~]# date
Fri  Aug  5  17:59:11  IST 2016

 

Example-2:

Print the date based on the string 2ndjuly 2016:

By using “-d” option we can print the absolute form of above mentioned date. Follow the following command to understand more.

[root@sys2 ~]# date -d "02  july  2016"
Sat  Jul  2  00:00:00  IST  2016

 

Note:

The above command will display the date based on the given string but not current date.

 

Example-3:

Print the date based on the file that contains some strings:

By using “-f” option we can give file as input to the date command. So I create a file called datefile with some dates as strings. See the following commands for more understanding.

[root@sys2 ~]# cat datefile
3/8/2002
6/8/2009
5/7/2001
12/26/2017
[root@sys2 ~]# date -f datefile
Fri  Mar   8   00:00:00  IST  2002
Mon  Jun   8   00:00:00  IST  2009
Mon  May   7   00:00:00  IST  2001
Tue  Dec  26   00:00:00  IST  2017

 

Finally all the strings absolute dates are printed successfully.

 

Example-4:

Print the date of next Friday:

This is also done by using “-d” option but the string will be different. See the following commands for more understanding.

[root@sys2 ~]# date -d "next fri"
Fri  Aug  12  00:00:00  IST  2016

 

So, from above output we came to know that counting from now the next Friday is 12th august.

 

Example-5:

Display last modified date of ‘/etc/passwd’ file:

By using ‘-r’ option with date command we can print the time stamp of the particular file.

Observe the follow to understand more.

[root@sys2 ~]# date -r /etc/passwd
Wed  Aug  3  12:30:46  IST  2016

 

From the above result the last modified date of /etc/passwd is 3rd august.

 

Example-6:

Set the time and date using the date command:

We use “-s” option with the date command to set the date and time of the machine. Observe the following to understand more.

[root@sys2 ~]# date
Fri  Aug  5  18:35:18  IST  2016

 

[root@sys2 ~]# date -s "Aug  7  12:35:22  IST  2017"
Mon  Aug   7  12:35:22  IST  2017

 

[root@sys2 ~]# date
Mon  Aug  7  12:35:31  IST  2017

 

Now, the date of the machine has successfully modified.

 

Exapme-7:

Print the universal time:

By using ‘-u’ option with date command we will display the universal time as follows.

[root@sys2 ~]# date -u
Fri   Aug  5  13:05:23  UTC  2016

 

Example-8:

Print full name of current month:

Simply use %B with date command it will give the full name of the current month.

[root@sys2 ~]# date +%B
August

 

Example-9:

Print full name of the weekday:

Simply use “%A” with date command it will produce the full name of the current day.

[root@sys2 ~]# date +%A
Friday

 

Example-10:

Print the date in year-month –day format:

Simply use “%F” with date command it will prints the date in year-month-day format.

[root@sys2 ~]# date +%F
2016-08-05

 

Topics Summary