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.
In this guide, we take a look at the date command in Linux and check out a few example usages.
The date command takes the following syntax:
$ date option format
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.
$date
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
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”
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”
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 as shown.
$ date + “%d %b %Y”
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’
You can print out the last time a file was edited or modified using the -r flag. In the example below, I have displayed the last time I modified a bash script file in my home directory.
$ date -r check_score.sh
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
To get more information about the command usage, run the command as shown.
$ date --help
Alternatively, you can visit the man pages to get more details about the date command and command options.
$ man date
In this guide, we have discussed the date command and showcased some of the command options that you can use to make the most of the command. We hope you found this guide useful.
I am a professional WordPress & Graphic designer. I have more than 6 years of experience in this field.
Welcome to the Ubuntu Weekly Newsletter, Issue 866 for the week of November 10 –…
Debian and Ubuntu are two popular Linux distributions. In this deep dive we will guide…
In this article, we will see how to Install Google Cloud BigQuery Python client library…
Nov 15,2024 Wallpaper Contest for Xfce 4.20 open for voting The submission phase for the…
MicroCloud 2.1.0 LTS is now available, expanding the number of Canonical infrastructure solutions with a…
Canonical is thrilled to be joining forces with Dell Technologies at the upcoming Dell Technologies…