Linux date command with example usages
- Linux date command with example usages
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.
The date command takes the following syntax:
$ date option format
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:
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
This article covers the date command in Linux and showcased some of the command options that you can use to make the most of the command. In fact, Linux date command displays and sets the system date and time. This command also allows users to print the time in different formats and calculate future and past dates.