How to Set, Change & Display Date in Linux

Try this guide with our instant dedicated server for as low as 40 Euros

change, set and display date in linux with date command

By providing your email address or using a single sign-on provider to create an account, you agree to our Terms of Service and that you have reviewed our Privacy Policy.

Thank you for contacting us! We will get in touch with you soon.

The date command in the Linux terminal displays or sets the system date and time. This built-in utility has various options and can output time and date information in several standard formats. 

Admins and power users can use this command in scripts to access the current data and time. However, the date is a very versatile command for finding out the current date/time and changing the system’s current time and date information. 

This comprehensive tutorial will help you understand how to set, change, and display dates in Linux. We’ll start with an overview of the date command and then discuss several use cases where you can use this command to change system dates and time information. 

Table of Contents

  1. Why Date Matters in Linux Distributions
  2. How to Set, Format & Display Date in Linux
    1. Prerequisites
    2. General Date Syntax in Linux
    3. Set a Date in Linux Manually
    4. Format Options for the Linux date Command
    5. Display Past Dates
    6. Display Future Dates
    7. Show the Date String From a File
    8. Show a File’s Last Modified Timestamp
    9. Change the Current System Time Zone
  3. Use the date Command in Scripts and SQL Statements
  4. Unix (Epoch Converter) Usage
  5. Conclusion
  6. FAQs

Why Date Matters in Linux Distributions

Time and date are critical information in the Linux environment for the following reasons: 

System Synchronization: Accurate timekeeping is essential for ensuring that events occur at designated times within systems and networks. This is crucial for maintaining data consistency and overall operational efficiency.

Log Management: Timestamps are crucial for recording record events, diagnosing problems, and evaluating system performance. Precise timestamps help determine the chronological order of events, simplifying diagnosis and event investigation.

Automation: System automation depends upon executing scripts and scheduled tasks at the designated date and time. If the system doesn’t keep the right time, workflows may be disrupted by tasks that execute inconsistently, either at the incorrect times or not at all.

Security: Accurate time is essential to operating many security measures, including time-based access controls. Maintaining system security requires keeping the system time accurate.

Data Consistency: Accurate timestamps are essential for preserving data consistency in database systems. Similarly, you need correct time and calendar information to facilitate efficient backup and recovery processes. Incorrect timestamps can lead to data loss or corruption.

Record-keeping: In various settings, from financial transactions to scientific research, accurate time and date management is essential for maintaining reliable records.

Compliance: Several sectors and regulatory organizations impose strict timestamp accuracy and retention rules. Compliance with these criteria is often mandatory.

Also Read: Get Current Date and Time in Python With 2 Easy Methods

How to Set, Format & Display Date in Linux

Now that you know the theory behind the need for keeping the right system time, let’s explore how to set, format, and display dates in a Linux environment. 

We’ll begin with the prerequisites to ensure we can execute the commands in this tutorial without any worries.

Prerequisites

  • A system running a Linux distribution.
  • An account with root privileges.
  • Access to the command line or terminal.

General Date Syntax in Linux

The Linux date command has the following basic syntax:

# date [option] [+format]

You can simply enter the date command at the terminal to display the current system time and date:

# date

date linux command

The output of the date command includes the current time with timezone, day of the week, day of the month, month, and year. This command displays the default time zone of the operating system.

You can format a date in the standard date display with the -d flag. For instance, the following command will display the supplied date in the standard long-form format: 

# date -d "2000-11-22 09:10:15"

date -d 2000-1122 091015 linux command

You can use the — date command to view a provided date string in a specific date format without changing the system’s actual date and time. For instance:

# date --date="09/10/1960"

date date 09-10-1960 linux command

Set a Date in Linux Manually

Use the set flag to manually adjust the system clock. For example, use the following command to set the system time to Nov 09, 2023, at 12:01 PM:

# date --set="20231109 12:01"

date set command in linux

When manually setting the system clock, exercise caution because most Linux distributions synchronize it with the system clock using the NTP or system-time synced services. 

Format Options for the Linux date Command

The Linux date command offers various options for customizing the display of dates and times. You can use these options to set specific formats that suit your specific operational requirements.

You can customize the output of the date command with patterns that format the date in the desired structure. These patterns (starting with the % symbol) act as placeholders that get substituted with current values when the command is executed. 

Let’s see an example where the corresponding control patterns substitute the current year, month, and day of the month:

# date +"Year: %Y, Month: %m, Day: %d"

date year month command in linux

Here are two more examples of formatting the date and time:

# date "+DATE: %D%nTIME: %T"

date time command in linux

# date +"Week number: %V Year: %y"

date week number linux command

The following table summarizes the most frequently used control patterns for the date command:

formatting characters for date command in linux

You can use man date or date– help to see all the formatting options for the command.

Display Past Dates 

Use the –date option to see dates in the past in Linux. This option is very flexible, and you can use values like tomorrow, Friday, last Friday, next Friday, and next week to finetune the output.

For instance, use the following command to print the date from two years ago:

# date --date="2 year ago"

date 2 year ago command in linux

Similarly, the following command prints the yesterday’s date:

# date --date="yesterday"

yesterday date command in linux

And, the following command will display the date (that will remain the same) and time from ten seconds ago:

# date --date="10 sec ago"

date 10 sec ago linux command

Display Future Dates 

Like the dates in the past, the date command with the –date option can show date and time information for the future. These commands are very similar to the commands discussed in the previous section. 

Let’s have some examples:

The following command displays the date and time for the next Monday:

# date --date="next monday"

next monday date command

Similarly, the following command displays the time and date for the fourth day from the time you execute the command:

# date --date="4 day"

date --date 4 day command in linux

Finally, the following command will display the date from tomorrow:

# date --date="tomorrow"

date --date tomorrow command in linux

Show the Date String From a File

You can format multiple dates contained in a file with the –file option. The syntax of the command is:

# date --file=file_name.txt

The output presents a date on a separate line. 

You can use the cat command to create a file containing date strings. The following command will take this file and output each date in the file as a separate line in the output.

# date --file=datefile 

date --file date file command in linux

Show a File’s Last Modified Timestamp

Displaying a file’s last modified timestamp is an important use case of the date command.

For this, use the -r flag to print a file’s last modification date. For instance, the following command will display the last modification date of the hosts file:

# date -r /etc/hosts

date -r -etc-hosts

Change the Current System Time Zone

The date command uses the time zone specified in /etc/localtime. You can change the current time of the system by editing the value of the TZ variable in this file. For instance, to set the system’s time zone to New York time, set the following value:

# TZ='America/New_York' date

change time zone linux command

You can reset (or reverse) this change with the simple date command. You can use the timedatectl list-timezones command to view all available time zones.

You can also use the date command to display the local time for a different time zone. For instance, to see the local time on the east coast of Australia at 4:30 PM on Monday, use::

# date -d 'TZ="Australia/Sydney" 04:30 next Monday'

date -d TZ Australia-Sydney 04 30 next Monday linux command

Use the date Command in Scripts and SQL Statements

You can use the date command for various date and time calculations in scripts and SQL statements. 

For instance, use the following command to format a MySQL backup file with the current date:

# mysqldump database_name > database_name-$(date +%Y%m%d).sql

Similarly, you can use the date command in shell scripts. In the following example, the output of the date command is assigned to a variable named date_now. This variable now holds the current date and time formatted per the specified format (“%F-%H-%M-%S“), making it convenient for further use within the script:

# date_now=$(date "+%F-%H-%M-%S")

Unix (Epoch Converter) Usage

An epoch converter is a tool that changes regular dates and times into a computer-friendly format called epoch time

Epoch time starts counting seconds from January 1, 1970. The date command can be utilized as an epoch converter. The number of seconds that have elapsed since January 1, 1970, at 00:00:00 UTC is known as the epoch or Unix timestamp.

Use the %s format control to display the number of seconds that have passed since the epoch to the present day:

# date +%s

# date +%s linux command

Press Enter to find the number of seconds that epoch to a given date has passed:

$ date -d "1984-04-08" +"%s"

date -d 1984-04-08 %s linux command

Conclusion 

Learning the Linux date command is essential for effectively managing time on your system. This tutorial helps you understand how to set, change, and display date information, ensuring reliable system performance and accurate timekeeping. You can use this command for a range of commands, such as automation, synchronization, log management, and formatting in a Linux environment.

Are you looking for the most economical dedicated server pricing? Look no further than our instant dedicated servers, available in various configurations, including 10Gbps dedicated servers and bare metal servers. RedSwitches is your trusted hosting partner, providing the right dedicated server for your needs.

FAQs

Q. What is Linux’s ‘date’ command, and why is it important?

Linux’s ‘Date’ command is a utility for managing date and time. It’s crucial for system synchronization, log management, and automation tasks.

Q. Can I use the’ date’ command to change the date and time?

Yes, the ‘date’ command allows you to change the date and time on your Linux system. 

Q. How can I set the date using the ‘date’ command?

You can set the date using the ‘date’ command by specifying the desired date and time formats. The guide provides detailed steps for this process.

Q. What are the advantages of formatting date and time with the ‘date’ command?

Formatting with the ‘date’ command provides flexibility in displaying date and time information according to specific preferences or requirements.

Q. Can the ‘date’ command be used for automation tasks?

Yes, the ‘date’ command helps automate tasks that involve date and time management. Understanding its usage is key for efficient scripting.

Q. Does changing the date/time affect system performance?

No, adjusting the date and time using the date command doesn’t cause any noticeable impact on system performance. It’s a routine task without any extra workload.

Q. How does the date command contribute to log management?

The date command ensures accurate timestamps in log files, aiding in troubleshooting, event tracking, and system performance analysis.

Q. Can the date command be utilized for scheduling tasks?

Yes, the date command is often used in scheduling tasks by scripting the execution of commands based on the specific date and time conditions.

Q. Are there any precautions when using the date command?

While the date command is powerful, users should exercise caution, especially when changing the system time. Understanding the command’s behavior is crucial to avoid unintended consequences.

Try this guide with our instant dedicated server for as low as 40 Euros