Date Command in Linux: How to Set, Change and Display Date

Lalita Rajpoot
5 min readSep 22, 2021

--

Introduction

Linux date command displays and sets the system date and time. This command also allows users to print the time in different formats and calculate future and past dates.

Prerequitites

  • A system running Linux
  • A user account with root privileges
  • Access to a terminal window/command line

Linux date Command Syntax

The syntax for the date command is:

date [option].. [+format]

To see all formatting options, run date --help or the man commandman date in your terminal.

How to Use date Command in Linux

To show the current system time and date, type in the date command:

date

The output displays the day of the week, day of the month, month, year, current time, and time zone. By default, the date command is set to the time zone of the operating system.

The -d option allows users to operate on a specific date. For example, we can type in the following command:

date -d "2021-09-22 1:10:10"

We can use the --date command to display the given date string in the format of a date. This command does not affect the system’s actual date and time values, and it only prints the requested date. For example:

date --date="1970/09/22"

Linux date Command Format Options

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"

Here are another examples:

date "+DATE: %D%nTIME: %T"
date +"Week number: %V Year: %y"
Week number

These are the most common formatting characters for the date command

  • %D – Display date as mm/dd/yy
  • %Y – Year (e.g., 2020)
  • %m – Month (01-12)
  • %B – Long month name (e.g., November)
  • %b – Short month name (e.g., Nov)
  • %d – Day of month (e.g., 01)
  • %j – Day of year (001-366)
  • %u – Day of week (1-7)
  • %A – Full weekday name (e.g., Friday)
  • %a – Short weekday name (e.g., Fri)
  • %H – Hour (00-23)
  • %I – Hour (01-12)
  • %M – Minute (00-59)
  • %S – Second (00-60)

Set or Change Date in Linux

To change the system clock manually, use the --set command. For example, to set the date and time to 5:30 PM, Sep 13, 2021, type:

date --set="20210923 05:30"

Display Past Dates

Use the --date option to display past dates in Linux. The date command accepts values such as "tomorrow", "Friday", "last Friday", "next Friday", "next week", and similar. So, use the following strings to print past dates::

date --date="2 year ago"
2 Year ago date
date --date="yesterday"
Yesterday date
date --date="2 sec ago"

Display Future Dates

The --date option can also display future dates. Like with past dates, you can type in strings to print upcoming dates:

date --date="next monday"
date --date="4 day"
date --date="tomorrow"

Display the Date String at Line of File

The --file option prints the date string present at each line of the file. Unlike the --date option, --file can present multiple date strings at each line.

This is the syntax for the --file command:

date --file=file_name.txt

Here we use cat command to add dates to a file and then print them with the date command:

Display Last Modified Timestamp of a Date File

When you use the -r option, the date command prints the last modification time of a file. For example, the following command prints the last time the hosts file was changed:

date -r /etc/hosts
Print last time date

Override a Time Zone

By default, the date command uses the time zone defined in /etc/localtime. To use a different time zone in the environment, set the TZ variable to the desired time zone.

For example, to switch to Asia time, enter:

TZ='India/Asia' date

The date command can also show the local time for a different time zone. For example, to display the local time for 4:30 PM next Monday on the Australian east coast, type:

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

Use date with Other Commands

You can use the date command to create file names that contain the current time and date. The input below creates a backup MySQL file in the format of the current date:

Another common use of the date command is in shell scripts. Below we assign the output of date to the date_now variable:

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

You now have a good understanding of how to use the date command in Linux.

Thank You:)

--

--

Lalita Rajpoot
Lalita Rajpoot

No responses yet