How To Show Date And Time In Linux Terminal Using Date Command?
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.
In this article, I’ll show how to use the date command in the Linux terminal to display date and time in different ways using several examples.
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) |
Display Date And Time Of Different Timezone
Like every command-line utility, the date command also provides several options to modify the output result. For instance, -u
, --utc
, or --universal
flag can be used with date command to print date and time in UTC (Universal Time Coordinate or Coordinated Universal Time).
$ date -u
Likewise, if you want to know the date and time of different timezone, you can also make use of the TZ environment variable with a date. Changing the value of TZ overrides the default timezone of your system.
$ TZ=America/Chicago date
Want to know the timezone information? You can run timedatectl
to know your default timezone and timedatectl list-timezones
to list all available time zones.
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"
Get The Last Modified Date Of A File
You can also use the Linux date command to display the date and time of the last modification of a file using -r
flag.
$ date -r <filename>