Get current date and time in Ubuntu command line
How To

Get Current Date and Time in Ubuntu Command Line

Sagar Sharma
Sagar Sharma

Table of Contents

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
Use the date command to get the date and time in terminal

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
Use the date command to get the current time and date in Ubuntu terminal

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"
Use the date command to only print date and time in 24hr format in Ubuntu terminal

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"
Get the 12hrs clock using the date command in Ubuntu terminal

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?

Set Date in Ubuntu Command Line
In some rare cases, you may want to set a different time and date in Ubuntu command line. Here’s how to do that.

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
Use the zdump command to get the current time and date in Ubuntu terminal

3. Using the hwclock command

📋
This method requires the user to have superuser (sudo) privileges.

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
Use the hwclock command to find the current time and date in Ubuntu terminal

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
Use the ntpdate command to find the current time and date in Ubuntu terminal

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}'
Print date and time only without NTP server IPs using ntpdate command

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
Use timedatectl command to find the current date and time in Ubuntu terminal

And there you have it.

On a similar note, you can learn about changing the timezone in Ubuntu.

How to Change Timezone in Ubuntu Command Line
Your remote Ubuntu server uses UTC and you have a difficult time figuring out the time difference? Change the timezone and live in peace.

I hope you like this tutorial. Let me know your views in the comment section.



Sagar Sharma

Sagar Sharma

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.