The date command is used to print the date in a specified format. The date command will print the date based on the /etc/localtime file. By default, it will print the date in the following format:
[day of week] [month] [day] [24 hour time ##:##:##] [time zone] [year] Wed Oct 31 15:03:16 GMT 2018
You can also format the time using a number of different formatting options. You initialize the formatting options with a plus sign (+), and each option is prefaced with a percent sign (%). For example, to retrieve the week number (out of 52 weeks a year), you’d enter date +%V
You can also use the date command to change the system’s date by including the -s option with a provided argument.
Syntax
The syntax of the date command is:
# date [options] [format]
Formatting Options
The following table lists some of the formatting options available.
Formatting Option | Prints |
---|---|
%A | The full weekday name. |
%B | The full month name. |
%F | The date in YYYY-MM-DD format. |
%H | The hour in 24-hour format. |
%I | The hour in 12-hour format. |
%j | The day of the year. |
%S | Seconds. |
%V | The week of the year. |
%x | The date representation based on the locale. |
%X | The time representation based on the locale. |
%Y | The year. |
date Command Examples
1, To display the date and time:
# date
2. To display the date and time entered by time:
# date --date=hh:mm:dd:mm:yy
3. To print the date from formats specified in each line of file:
# date -f file.txt # date --file file.txt
4. To get the last modification time for a file:
# date file.txt
5. To output date and time in RFC 2822 format:
# date -R # date --rfc-2822
6. To output date and time in RFC 3339 format:
# date --rfc-3339
7. To set the time specified by the time:
# date -u # date --utc # date --universal
8. To display the version information:
# date --version
9. Display the current date using the default locale’s format:
# date +"%c"
10 .Display the current date in UTC and ISO 8601 format:
# date -u +"%Y-%m-%dT%H:%M:%SZ"
11. Display the current date as a Unix timestamp (seconds since the Unix epoch):
# date +%s
12. Display a specific date (represented as a Unix timestamp) using the default format:
# date -d @1473305798
13. Convert a specific date to the Unix timestamp format:
# date -d "2018-09-01 00:00" +%s --utc
14. Display the current date using the RFC-3339 format (`YYYY-MM-DD hh:mm:ss TZ`):
# date --rfc-3339=s
15. Set the current date using the format `MMDDhhmmYYYY.ss` (`YYYY` and `.ss` are optional):
# date 093023592021.59