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

October 1, 2020

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.

Read on to learn how to use the date command in Linux.

How to use Linux date command

Prerequisites

  • 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]

How to Use date Command in Linux

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

date
Date command in linux

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 "2000-11-22 09:10:15"
See an older date

You 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="09/10/1960"
Pull a date from a string

Note: Learn how can you create a script using the printf command to display the current date.

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"
Format the current date

Here are another two formatting examples:

date "+DATE: %D%nTIME: %T"
Another date format example
date +"Week number: %V Year: %y"
Another formatting example

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)

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

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, May 13, 2010, type:

date --set="20100513 05:30"
Manual time set in Linux

Most Linux distributions have the system clock synchronized using the ntp or the systemd-timesyncd services, so be careful when the setting the clock manually.

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"
Format to "Date two years ago" in linux
date --date="yesterday"
Yesterday's date format in linux
date --date="10 sec ago"
Time ten seconds ago format in linux

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 next monday format in linux
date --date="4 day"
Date four days from now format in linux
date --date="tomorrow"
Tomorrow's date format in linux

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 the cat command to add dates to a file and then print them with the date command:

Display dates at each file line in linux

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
The last date the file was modified in linux

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 New York time, enter:

TZ='America/New_York' date
Switch to New York time zone in Linux

Type in the date command to return the system to its default time zone. To see all available time zones, use the timedatectl list-timezones command.

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'
Switch to South Australian time in Linux

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:

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

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")
Echo the date now variable

Use Unix Epoch Time (Epoch Converter)

You can use the date command as an Epoch converter. Epoch, or Unix timestamps, is the number of seconds that have passed since January 1, 1970, at 00:00:00 UTC.

To show the number of seconds from the epoch to the current day, use the %s format control:

date +%s
Epoch seconds in linux

To see how many seconds passed from epoch to a specific date, enter:

$ date -d "1984-04-08" +"%s"
Epoch seconds from a specific date in Linux

Conclusion

You now have a good understanding of how to use the date command in Linux. If you are interested in more date/time configuration options for Linux, read How to Set or Change Timezone/Date/Time on Ubuntu.

Was this article helpful?
YesNo
Andreja Velimirović
Andreja is a content specialist with over half a decade of experience in putting pen to digital paper. Fueled by a passion for cutting-edge IT, he found a home at phoenixNAP where he gets to dissect complex tech topics and break them down into practical, easy-to-digest articles.
Next you should read
How to Use Linux dig Command (DNS Lookup)
September 1, 2020

dig (Domain Information Groper) command is a tool for querying DNS name servers. It is a helpful command for...
Read more
22 Best Linux Text Editors for Programming & Coding
September 3, 2019

A text editor is an application that lets you type text. All Linux distributions come with built-in editors...
Read more
How to Create a File in Linux Using Terminal/Command Line
June 27, 2019

Creating a file in Linux might seem straightforward, but there are some surprising and clever techniques...
Read more
How To Use grep Command In Linux/UNIX
March 28, 2019

This guide details the most useful grep commands for Linux / Unix systems. After going through all the...
Read more