Once in a while, you will need to probe the system date and time. The Linux date command is a command-line tool used to display the time and date on a Linux system. It comes preinstalled in all Linux distros, and therefore, there is no need to install it.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related System tools Configuration queries.
In this context, we shall look into the date command in Linux as well as a few example usages.
Linux date command Syntax
The date command takes the following syntax:
$ date option format
Example of using date command in Linux
1. How to Display system date without command arguments ?
In its basic form, the date command displays the system date and time as follows. It displays a comprehensive date which includes the day of the week, followed by the month and year. This is then followed by the system time and timezone towards the end of the output:
2. How to Display time in UTC format ?
UTC time is short for Coordinated Universal Time. This was formerly known as the GMT time and is the primary time standard that the world's timezones are based on.
To display the UTC time, run the command:
$ date -u
3. How to Display future dates ?
The date command can also be used to perform some pretty cool operations. For example, you can display future dates and times using the -d option.
For example, to check tomorrow's date and time, run the command:
$ date -d "tomorrow"
To check the date and time exactly a week from now, execute:
$ date -d "next week"
To check the date on a future day of the week, say Monday, run:
$ date -d "next Monday"
4. How to Display past dates ?
Conversely, you can display past dates and times. For example, to display yesterday’s date run the command:
$ date -d "yesterday"
To display the date and time last Friday, execute:
$ date -d "Last Friday"
5. How to Display custom date formats ?
There is a multitude of options that are available for use. Here are some of the options and what they stand for:
- %a – Displays a day of the week in short format ( for instance, Tue).
- %A – Displays the full name of the day of the week ( e.g., Tuesday ).
- %b – Displays the month of the year in short format( e.g., Feb ).
- %B – Displays the full name of the month ( e.g, February ).
- %d – Displays the day of the month in numeric format ( for example 15 ).
- %H – Displays time in 24hr format ( 00 .. 23 ).
- %I – Displays time in 12hr format ( 01 .. 12 ).
- %J – Displays the day of the year in numeric format ( 001 .. 366 ).
- %m – Displays the months of the year in numeric format ( 01 .. 12 ).
- %M – Prints out the minutes ( 00 .. 59 ).
- %S – Prints out the seconds ( 00 .. 60 ).
- %u – Displays the days of the week in numeric format ( 1 .. 7).
- %Y – Displays the year in Full (e.g, 2022).
With these options, you can print out a custom date format. For example, to display the date along in YY-mm-dd format, run the command:
$ date + "%Y-%m-%d"
Let's check out another example. You can print out the date that includes a combination of string and numeric values:
$ date + "%d %b %Y"
6. How to Read date from a string value and print it in a human-readable format ?
Sometimes, you might want to render a date in a more intuitive or human-readable format. The -d option also reads a date in a string value and prints it out in a more human-readable or simple format:
$ date -d "2021-11-09 15:30:47"
In addition, you can employ custom formatting rules like we discussed earlier in the previous sub-topic:
$ date -d '5 Dec 2021' +'%A, %d %B %Y'
7. How to Display a file's last modification date ?
You can print out the last time a file was edited or modified using the -r flag. In the example below, We have displayed the last time we modified a bash script file in my home directory:
$ date -r check_score.sh
8. How to Display dates in other Timezones ?
The date command can also be used to display time in other timezones using the TZ environment variable.
To list the available time zone, run the timedatectl command as follows:
$ timedatectl list-timezones
To print the time in another time zone, for instance, Dubai, run the command:
$ TZ='Asia/Dubai' date
9. How to Get help with more command options ?
To get more information about the command usage, run the command:
$ date --help
Alternatively, you can visit the man pages to get more details about the date command and command options:
$ man date
[Need assistance in fixing Linux system issues ? We can help you. ]