Get Current Time in Linux

For the system administrators, time is the key because they need to maintain accurate logs, which require the proper synchronization of the date & time of all systems in the network. It also helps them keep track of the files when they were created, accessed, or modified, and also, they can monitor user activity like logins, logouts, and other activities. The time needs to be synced, so you should know the time on your system.

After going through this guide, the audience will understand the following ways to get the current time in Linux.

How to Get the Current Time Using the date Command?

The “date” command in Linux can either display or set the time or date, and it does that in different formats using flags/options. Here’s how you can view/get the current time on your system.

$ date

The above command shows the current date and time while the “CST” indicates the Central Standard Time zone.

Viewing the Time in UTC

Use the “-u” flag like this to view the time & date in Coordinated Universal Time (UTC).

$ date -u

The above command shows the current date and time while the “UTC” indicates the time in the Coordinated Universal Time zone, which is the global time standard.

Viewing the Time in a Specified Format

Using the “+” option, you can view the data/time in a specified format. For example, the “+%d-%m-%Y %H:%M:%S” will display the date and time in the format DD-MM- YYYY HH:MM:SS.

$ date +%d-%m-%Y-%H:%M:%S

The above command allows users to view the time/date in “DD-MM-YYYY – HH-MM-SS,” and you can specify any format in which you want to see it.

Modify the Current Date/Time

Using the “-s” flag, you can easily modify the time, which is helpful when changing the current time or date, but this requires sudo/root privileges. Here we’d set the date and time to “2022-12-31 11:59:59.”

$ sudo date -s '2022-12-31 11:59:59'

The above image shows that the date and time are successfully set to “2022-12-31 11:59:59.”

Viewing the Current time in a 24-Hour Format

The“%T” flag of the date command displays the current time in 24-hour format (HH:MM:SS).

$ date +”%T”

This is the most simplified method to view only the current time in Linux, and the “-r” flag can be used to view the time in 12-hour (HH:MM:SS) format.

$ date +”%r”

The above command, when executed, displays the current time in a 12-hour format.

To know more about the flags/options of the date command, use this help command.

$ date --help

How to Get the Current Time Using the timedatectl Command?

The timedatectl is a powerful command line tool used to get information related to the system’s time and manage it in a detailed view. To view the time and date using the timedatectl command, execute this.

$ timedatectl

Here’s a breakdown of the above output.

  • Local time represents the current date and time in the local time zone, Central Standard Time (CST).
  • Universal time represents the current date, and UTC is the time standard (global).
  • RTC time shows the date and time of the Real-Time Clock.
  • Time zone stands out for the current time zone set.
  • System clock synchronized indicates if the system clock is synchronized with a time source, i.e., the NTP service, which is “yes.”
  • NTP service shows the status of Network Time Protocol (NTP) which is responsible for keeping the system clock synchronized.
  • RTC in local TZ indicates if the Real-Time Clock (RTC) is configured or not to set the time according to the current time zone. It is currently set to UTC.

Get Complete Information About the Current Time

The flag “timedatectl status” shows detailed information relative to time and date (same as the timedatectl command).

$ timedatectl status

The above command, when executed, shows the same output as timedatectl with no flags, which is explained in the above section in detail.

Viewing the Settings Related to the System’s Time

The “show” flag of timedatectl is used to view detailed information about the system’s time settings.

$ timedatectl show

Here is the breakdown of the above output.

  • Time zone stands out for the current time zone.
  • RTC in local TZ indicates if the Real-Time Clock (RTC) is configured or not to set the time according to the current time zone. It is currently set to UTC.
  • CanNTP indicates the system’s capability to synchronize 
  • NTP service shows the status of Network Time Protocol (NTP) which is responsible for keeping the system clock synchronized.
  • TimeUSec represents the local (current) time zone’s time and date.
  • RTCTimeUSec represents the current date and time of the Real Time Clock (RTC).

There are many other valuable flags of the timedatectl command whose information can be viewed by executing this help command.

$ timedatectl --help

How to Get the Current Time Using the hwclock Command?

Using the “hwclock” command of Linux, the current time is shown by reading the hardware (BIOS) clock.

The “-r” flag of the hwclock command views the current time set on the system’s hardware.

$ sudo hwclock -r

The above output isn’t the standard time format, and here’s the breakdown.

  • 2023-01-17 is the date in YYYY-MM-DD format.
  • 00:21:57 is the time in HH:MM:SS while the .433476 is the 6-digit microseconds
  • -06:00 is the time offset from UTC in HH:MM format.

Conclusion

The current time in a Linux system can be obtained using the date, timedatectl, and the hwclock command. These command line utilities offer detailed information about the time using their supported flags. This post has explained the methods to get the current time in Linux.