Does anybody know what time it is?

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,678
Reaction score
3,498
Credits
32,826

Viewing and Managing Date and Time on a Linux Computer​

Viewing the Date and Time​

To view the current date and time on a Linux computer, you can use the date command. Simply open your terminal and type:

Code:
 date

This will display the current date and time in the default format.

Changing the Output Format​

You can customize the output format of the date command using format specifiers. Here are a few examples:

  1. Display the date in YYYY-MM-DD format:
    Code:
     date +%F
  2. Display the time in HH:MM:SS format:
    Code:
     date +%T
  3. Display the full date and time in a custom format:
    Code:
     date +"%A, %B %d, %Y %H:%M:%S"

Setting the Date and Time Manually​

To set the date and time manually, you need superuser privileges. Use the date command with the -s option followed by the desired date and time in quotes. For example, to set the date and time to January 9, 2025, 12:00 PM:

Code:
 sudo date -s "2025-01-09 12:00:00"

After setting the system time, you may also want to update the hardware clock to match the system clock:

Code:
 sudo hwclock --systohc

Using timedatectl​

  1. Display Current Date and Time:
    Code:
     timedatectl
    This command shows the current local time, universal time, time zone, and NTP synchronization status.
  2. Set the Time Zone:
    Code:
     sudo timedatectl set-timezone America/New_York
    This sets the system's time zone to New York.
  3. Enable NTP Synchronization:
    Code:
     sudo timedatectl set-ntp true
    This enables automatic synchronization of the system clock with NTP servers.
  4. Set the Date and Time:
    Code:
     sudo timedatectl set-time "2025-01-09 12:00:00"
    This sets the system date and time to January 9, 2025, 12:00 PM.

Using hwclock​

  1. Display Hardware Clock Date and Time:
    Code:
     sudo hwclock --show
    This command displays the current date and time of the hardware clock.
  2. Set Hardware Clock to System Time:
    Code:
     sudo hwclock --systohc
    This sets the hardware clock to the current system time.
  3. Set System Time to Hardware Clock:
    Code:
     sudo hwclock --hctosys
    This sets the system time to the current hardware clock time.
  4. Set Hardware Clock Manually:
    Code:
     sudo hwclock --set --date "2025-01-09 12:00:00"
    This sets the hardware clock to January 9, 2025, 12:00 PM.

Network Time Protocol (NTP)​

NTP (Network Time Protocol) is a networking protocol used to synchronize the clocks of computer systems over packet-switched, variable-latency data networks. NTP can synchronize time to within a few milliseconds of Coordinated Universal Time (UTC). It typically uses a hierarchical system of time sources, with each level referred to as a "stratum."

Your local router often acts as an NTP server, providing time synchronization services to devices on your network. This ensures that all devices have consistent and accurate time settings.

Timezone Files​

Linux stores timezone information in the /usr/share/zoneinfo directory. Each file in this directory represents a specific timezone and contains information about time offsets, daylight saving rules, and historical changes associated with that timezone.

To set the timezone, you can create a symbolic link from /etc/localtime to the appropriate timezone file. For example, to set the timezone to New York:

Code:
 sudo ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime

You can also use the timedatectl command to set the timezone:

Code:
 sudo timedatectl set-timezone America/New_York

Conclusion​

Managing date and time on a Linux computer is straightforward with the date command and other utilities like hwclock and timedatectl.
 


Members online


Top