How To Show Date And Time In Linux Terminal Using Date Command?

How To Display 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
Display date and time

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"
Display date and time with user-defined format
Display date and time with user-defined format

Here are the important format controls available to display information:

CharacterDescription
%aLocale’s abbreviated weekday name (e.g., Sun)
%ALocale’s full weekday name (e.g., Sunday)
%bLocale’s abbreviated month name (e.g., Jan)
%BLocale’s full month name (e.g., January)
%dDay of month (e.g., 01)
%HDisplay hour in 24-hour format
%IDisplay hour in 12-hour format
%mMonth (01..12)
%MMinute (00..59)
%n%ZA newline
%pLocale’s equivalent of either AM or PM; blank if not known
%SSecond (00..60)
%YYear
%ZAlphabetic 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
UTC date and time
UTC date and time

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
Print date and time of different timezone
Print date and time of different timezone

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.

Print your default timezone
Print your default timezone

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"
Past date and time using date command
Past date and time using date command

Future Date And Time

$ date --date="tomorrow"
$ date --date="next day"
$ date --date="10 day"
$ date --date="10 year"
Future date and time using date command
Future date and time

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>
Last modified date of a file
Last modified date of a file

Similar Posts