Get Current Date and Time in Ubuntu Command Line
Here are several ways to get the current date and time in Ubuntu command line.
There are times when you want to know the date and time via the terminal, especially when you don't have access to the desktop environment.
The easiest way to get the current date and time is to use the date
command (without any options):
date
Pretty easy. Right?
Want more ways to get the current date and time? Here you have it.
How to get the current date and time in Ubuntu
In this tutorial, I will walk you through the following methods to get the current date and time in the Ubuntu terminal:
- Using the date command
- Using the zdump command
- Using the hwclock command
- Using the ntpdate command
- Using the timedatectl command
So let's start with the first one.
1. Using the date command
As I mentioned in the beginning, the date command is the easiest method to get the current date and time where all you have to do is execute the date
command in the terminal:
date
By default, it will show the day of the week, month, date, and time including minutes and seconds, timezone, and year.
That's a lot of info! And you may not want to have it all the time.
Worry not. The date command lets you customize the output as per your needs. For example, here, I only went for the date and time in 24hr format:
date "+%H:%M:%S %d/%m/%y"
Here,
%H
: Used to print hours in 24hr format%M
: Shows minutes%S
: Prints seconds%d/%m/%y
: Shows date, time, and year respectively.
And if you want a 12hrs clock, you can use the following:
date "+%r %d/%m/%y"
But what if you want to get only the date or only the time? Well, you can pull that off using date command easily.
Get the date only
If you want to print only the date without any additional information, execute the date command in the following manner:
date +"%d/%m/%y"
Get current time only
If the script demands the current time without any additional information or you are a nitpicker for output on the terminal, it can be done using the following:
date +"%H:%M:%S"
And if you want time in 12 hours format, use this:
date "+%r"
Did you know that the same date command can also be used for setting the date in Ubuntu?
2. Using the zdump command
The zdump command is used to dump the time specific to the time zone which simply means you can know the current time of your timezone and of the others as well.
To use the zdump, you have to follow the given command syntax:
zdump timezone
As I'm from India, then I will be using Asia/Kolkata as a timezone:
zdump Asia/Kolkata
3. Using the hwclock command
Here, hwclock reads for hardware clock which is able to get you the perfect time even if you're offline and don't need to sync to the network as it is directly related to computer hardware.
Also, the execution is super simple. All you have to do is execute the hwclock
with sudo as shown here:
sudo hwclock
In case you're confused about those decimals (.778051 in my case), they are nothing but fractional sections.
And +5:30
indicates the timezone offset.
4. Using the ntpdate command
The ntpdate
command is used to set the system time using the NTP server and can also be used to print the current time.
But it does not come pre-installed in Ubuntu and you can use the following command to install the ntpdate
command in Ubuntu:
sudo apt install ntpdate
Once done, use the following command to find the current time and date:
ntpdate -q time.google.com
As you can see, it listed the time and date along with the IPs of the NTP servers.
But if you're a normal user, then it won't add much value. And if you only want the date and time output, then you can use the following command:
ntpdate -q time.google.com | grep -oP '\d{2} \w{3} \d{2}:\d{2}:\d{2}'
Here, the ntpdate command was piped with the grep command and I used the regular expression \d{2} \w{3} \d{2}:\d{2}:\d{2}
to match the date and time.
5. Using the timedatectl command
The timedatectl command is used to change the system time and can also be useful when you want to know the current time.
All you have to do is execute timedatectl
in terminal:
timedatectl
And there you have it.
On a similar note, you can learn about changing the timezone in Ubuntu.
I hope you like this tutorial. Let me know your views in the comment section.
A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.