Linux Commands

Linux Get Current Time

The time function is utilized within all operating systems like Windows, Linux, Unix, etc. You may see the current date and time at the desktop screens of your operating system within standard formats. But what about the exact current time displayed on the Linux operating system. If you are looking for a guide toward using date and time functions to display the current date and time in the Linux shell, then this article is for you. So let’s start this guide by logging in to your Ubuntu 20.04 system, as we will be performing each command on the Ubuntu 20.04 Linux shell.

After a successful login, you first need to open the Ubuntu’s terminal shell via the activity area at the desktop taskbar. Tap on it and write “terminal” in the search bar shown on your screen. The pop-up terminal screen will be shown, and you have to tap on it to open it quickly. If this process is lengthy, try using the “CTRL+Alt+T” to launch it faster. Now, your terminal will be opened within no more than 10 seconds on your screen. Let’s start with the most basic command to display the date and time of our current time zone on the shell. On executing the following command, the time has been displayed in the “hour: minute: second” format along with the time zone, i.e., PKT. It also shows the current date according to the time zone. Upon execution, you will get the output, as shown below:

$ date

If you want to only display the date on your shell with the specific format, you need to specify the format in the date command. Use the inverted commas to add the format in “%d” for day, %m for month, %y for year separated by “-” signs. This command execution shows us the date in standard “day-month-year” format. Upon execution, you will get the following output:

$ date + “%d-%m-%y”

If you only want to display the current time on your shell using the “date” command, you must use the “+%T” character flag. On execution of this command, the current time for a specific time zone will be displayed in a standard format, i.e., “hour: minute: second”. Upon execution, you will get the output, as shown below:

$ date “+%T”

If you want to display both the current and time in a single line with the time and date specification, you can also do that with the date command. So, to display the title “Date” and “Time”, we need to add “+DATE: %D” for date and “TIME: “%T” for time. The output of this instruction shows the date and time in standard format with titles of date/time on the shell. Upon execution, you will get the following output:

$ date “+DATE: %D, TIME: %T”

For example, we want to get the exact date and time of the same time zone and same time for some past year. We need to utilize the “—date” flag with the “=” sign to get the value to be searched. For example, we want to get a date and time exactly three years back for the same moment. On execution, the following instruction shows the date and time for the exact three years back, i.e., February 27, 2019:

$ date - - date=“3 years ago”

Upon execution, you will get the affixed output.

If we want to take an exact date and time for the very next day on the shell, we will use the same “date” command with the “—date” flag. Use the value “tomorrow” in the inverted commas and execute the command. This will show the next exact date from the current exact date and time, i.e., February 28, 2022.

$ date - - date= “tomorrow”

Upon execution, you will get the following output:

$ date ‘+%A %W %Y %X’

Upon execution, you will get the following output:

$ date +%C

Upon execution, you will get the following output:

$ date +%T

Upon execution, you will get the following output:

$ date “+%H:%M:%S”

Upon execution, you will get the following output:

Another command is also known to get the current date and time for the current time zone of Linux. This command is the “timedatectl” instruction of Bash. It will not only show you the current local time but also the universal time, RTC time, your current time zone, and if the NTP services is enabled on your system. The execution of this command shows all the mentioned specifications on the shell, i.e., time and date. Upon execution, you will get the output, as shown below:

$ timedatectl

Let’s use the time zone date command to get the current time of our choice of time zone. So, we have to use the “TZ” variable with the “=” sign to get the time zone value. We want to get the current time for “Asia/Karachi” this time. The use of the “date” keyword with the “+%T” is necessary for fetching time for this time zone. We have the time displayed for the “Asia/Karachi” time zone upon execution. If you want to get the value for another time zone, use the specific time zone as a value to TZ. Let’s say we have been using the time zone “Asia/Istanbul” to get the current time for Istanbul, Turkey. The instruction shows the time for “Istanbul, Turkey” on the shell. Upon execution, you will get the following output:

$ TZ= “Asia/Karachi” date “+%T”

$ TZ= “Asia/Istanbul” date “+%T”

Conclusion:

In this article, we have tried to implement almost all the commands to get the current date and time for our current time zone. We have also tried to get the current time for other time zones, the past time and date, the coming date and time, and many more. You can also use %r and %R to get the current time. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.