Skip to Content

3 ways to get time in Linux

There are a few different ways to check the time on your Linux machine. The first way is to use the date command. This command displays the current date and time in human-readable format.

The second way is to use the timedatectl command. This command allows you to view and change the system time and date settings.

The third way is to use the hwclock command. This command shows or sets hardware clock information on Linux systems.

If you’re new to the world of Linux, you can refer to “Mastering the Linux Command: A Beginner’s Guide.”  This in-depth article will be your guiding light, providing you with the foundational knowledge and skills required to navigate and wield the Linux command line interface (CLI) effectively and with assurance.

Procedure to check time in Linux

The procedure to get time in Linux is as follows:

  • Open the terminal.
  • Type date or timedatectl command
  • Press Enter to run the command.
  • The command output displays the time in human-readable format.

 

This article is part of the following series.

 

Get time with date command in Linux

To get the current time using the date command in Linux, simply open a terminal and enter the following command:

date

Running this command will display the current date and time in the default format.

For example, the output may look like this:

Fri Jun 18 15:30:45 UTC 2023

The date command provides various options to customize the output format. For instance, you can use the %T format specifier to display the time in the HH:MM:SS format.

Here’s an example:

date +%T

Output:

15:30:45

To format the date command’s output, you can use control characters preceded by a + sign.

Format controls begin with the % symbol and are substituted by their current values.

Here, the %Y character is replaced with the current year,%m with month, and %d with the day of the month:

date +"Year: %Y, Month: %m, Day: %d"
Year: 2022, Month: 04, Day: 16

Here are more examples of time format.
Command: $date "+%D"
Output: 10/11/30
Command: $date "+%D %T"
Output: 10/11/30 16:13:27
Command: $date "+%Y-%m-%d"
Output: 2030-10-11
Command: $date "+%Y/%m/%d"
Output: 2030/10/11

The following are the date format options.

$date +%[format-option]

Format Option Description
%D Display date as mm/dd/yy.
%d Display the day of the month (01 to 31).
%a Displays the abbreviated name for weekdays (Sun to Sat).
%A Displays full weekdays (Sunday to Saturday).
%h Displays abbreviated month name (Jan to Dec).
%b Displays abbreviated month name (Jan to Dec).
%B Displays full month name (January to December).
%m Displays the month of the year (01 to 12).
%y Displays last two digits of the year (00 to 99).
%Y Display four-digit year.
%T Display the time in 24-hour format as HH:MM:SS.
%H Display the hour.
%M Display the minute.
%S Display the seconds.

Related: 2 ways to change timezone in Linux

 

Get time with timedatectl command in Linux

We can also use timedatectl command to get time in Linux. This command allows you to view and change the system time and date settings.

To use the timedatectl command, type “timedatectl” at the command prompt. This will display the current system time and date settings.

$ timedatectl
Local time: Sat 2022-04-16 13:06:00 GMT
Universal time: Sat 2022-04-16 13:06:00 UTZ
RTC time: Sat 2022-04-16 13:02:34
Time zone: GMT (GMT, +0000)
System clock synchronized: yes
NTP service: active
RTC in local TZ: yes

To change the system time, use the set-time option. For example, to set the system time to 12:00pm on January 22nd, 2030, you would type the following command:

timedatectl set-time "2030-01-22 12:00:00"

related: 3 ways to check process start time in Linux

Find time with hwclock command in Linux

We can also check the time in Linux with the hwclock command. This command shows or sets hardware clock information on Linux systems.

The hardware clock is a clock that is set by the BIOS and is used to keep track of the time when the system is turned off. The system time is the time that is set by the operating system and is used when the system is turned on.

To use the hwclock command, type “hwclock” at the command prompt. 

This command will show the current date and time stored in the hardware clock. The output will be in a format like this:

2023-06-18 15:30:45 UTC

To set the system time, use the –set option. For example, to set the system time to 12:00pm on January 22nd, 2019, you would type the following command:

hwclock --set --date "2030-01-22 12:00:00"

related: 3 Ways to get file modified time in Linux

Understanding time in Linux

In Linux, time is measured in seconds since the Unix epoch. The Unix epoch is January 1, 1970 UTC. This means that the time on your Linux machine is always relative to this date.

Checking the time on your Linux machine is important for a number of reasons. For example, if you are running a cron job, you will want to make sure that the time is set correctly so that the job will run at the correct time.

Additionally, if you are troubleshooting an issue on your machine, checking the time can be helpful in determining when the issue occurred.