This command is used to display the date and time of the machine and to modify the date of the machine.
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.
Print current date and time in the machine:
Simply use the date command to print the current date of the system.
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.
The above command will display the date based on the given string but not current date.
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.
Finally all the strings absolute dates are printed successfully.
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.
So, from above output we came to know that counting from now the next Friday is 12th august.
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.
From the above result the last modified date of /etc/passwd is 3rd august.
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.
Now, the date of the machine has successfully modified.
Print the universal time:
By using ‘-u’ option with date command we will display the universal time as follows.
Print full name of current month:
Simply use %B with date command it will give the full name of the current month.
Print full name of the weekday:
Simply use “%A” with date command it will produce the full name of the current day.
Print the date in year-month –day format:
Simply use “%F” with date command it will prints the date in year-month-day format.