Exploring DATE command in Linux
The date
is a command-line utility for Unix-like operating system to display date and time. Not just to view, you can also use the same command to set system date and time as well.
Display Current Date And Time
Running date
command in a console prints the current date and time in a default format string (“+%+”). It reads data from the kernel clock.
$ date
Display Date And Time In User-Defined Format
If you don’t like the default output format “+%+”, you can also change it and display it in your own format. What you need to do is write your own format using different format controls in a sequence.
$ date "+DATE: %d-%m-%Y%nTIME: %H:%M:%S %r %Z%nDAY: %A"
Here are the important format controls available to display information:
Character — — — — — — -Description
%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)
%d — — — — Day of month (e.g., 01)
%H — — — — Display hour in 24-hour format
%I — — — — Display hour in 12-hour format
%m — — — — Month (01..12)
%M — — — — Minute (00..59)
%n%Z — — — — A newline
%p — — — — Locale’s equivalent of either AM or PM; blank if not known
%S — — — — Second (00..60)
%Y — — — — Year
%Z — — — — Alphabetic time zone abbreviation (e.g., EDT)
Get Past And Future Date
Using --date
or -d
option, you can also display the past and future dates by giving a date string in the format of a date.
Past Date And Time
$ date --date="5 years ago"
$ date --date="5 sec ago"
$ date --date="yesterday"
$ date --date="5 days ago"
Future Date And Time
$ date --date="tomorrow"
$ date --date="next day"
$ date --date="10 day"
$ date --date="10 year"