Linux logo

What is date Command in Linux?

The date command, a simple yet powerful tool, is your gateway to unveiling and customizing the time on your Linux system. By default, it displays the current time and date, but its capabilities extend far beyond.

Customization is the key, allowing you to present dates in diverse formats, perform calculations, and even take control of the system clock with administrator privileges. No matter your Unix-like system, the date command is a universal companion, prominently featured in Linux distros.

This tutorial provides practical examples, guiding you to harness the full potential of the date command, making time a tool at your command.

Syntax of date command

The “date” tool uses the following command arrangement.

date [OPTION]… [+FORMAT]
date [-u|–utc|–universal] [MMDDhhmm[[CC]YY][.ss]]

Let us look at a few examples of using the date command.

Using the simple date command example

The simple date command will show you the current date and time of the system.

$ date
Tue Oct 15 6:53:22 PST 2019
How to specify the date format

If you want the “date” command to run on a particular date, use the “-d” flag. For example,

$ date -d "<year>-<month>-<date>"

An example can be:

$ date -d 2015-6-20
Formatting date in dd-mm-yy format example

You can format the date as follows in the dd-mm-yy format:

$ date +"%d-%m-%y"

For example:

20-06-19

Output the previous date example

To print the date of the day before yesterday, run:

date --date='2 days ago'

Previous month example

$ date -d last-month

Next year example

$ date -d next-year

Next week example

$ date -d next-week
An example of a full month name

Display the current full month name and the day of the month:

$ date '+%B %d'
List of Format specifies used with date command
Format 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 year (01 to 12).
%y Displays last two digits of the year(00 to 99).
%Y Screen four-digit year.
%T Display the time in 24 hour format as HH:MM:SS.
%H Display the hour.
%M Display the minute.
%S Screen the seconds
Further Reading:  Know Which Linux Version You Are using: 5 Ways
A few examples of commands with specifiers

Try these commands to see the output yourself:

$ date "+%Y-%m-%d"

Date in mm/dd/yy format command

$ date "+%D"

More

$ date "+%D %T"

$ date "+%Y/%m/%d"

Try various combinations to check it yourself.

How to Check file modification time

The “date” is incredibly versatile. This tool is also used to look at the time when a particular file/folder was last modified.

date -r <file>
Overriding the time zone example

By default, whenever running date, it is going to use the current time zone. The time zone is set from the environment variable TZ. The default value of TZ is defined in the file “/etc/localtime”.

By changing the value of TZ, we could induce date to demonstrate the time of different time zones. For example.

TZ=GMT+2 date
Setting the time example

As mentioned in the introductory part, you may also set the time by using the date command.

Set Time and Date using –set option You can set time and date of your system using -s or –set option as shown below:

$ date -s "Tue Dec 17 21:00:00 PDT 2019"

Best Practices of Using Date Command in Linux

Display Current Date and Time:Use the simple date command to view the current date and time.

$ date

Specify Date Format:

To run the date command for a specific date, utilize the -d flag.

$ date -d “<year>-<month>-<date>”

Example:

$ date -d 2015-6-20

Custom Date Format:

Format the date as needed, for instance, in the dd-mm-yy format.

$ date +”%d-%m-%y”

Example:

$ date +”%d-%m-%y”

Display Previous Date:

Print the date of the day before yesterday.

$ date –date=’2 days ago’

Display Previous Month:

Show the date for the last month.

$ date -d last-month

Display Next Year:

View the date for the next year.

$ date -d next-year

Display Next Week:

Present the date for the next week.

$ date -d next-week

Display Full Month Name:

Display the current full month name and the day of the month.

$ date ‘+%B %d’

Use UTC Time:

When required, display the date in Coordinated Universal Time (UTC) using the -u or –utc option.

$ date -u

Calculate Future Date:

Further Reading:  What is tar Command in Linux?

Calculate a future date by specifying the desired date in the MMDDhhmm[[CC]YY][.ss] format.

$ date 112518002021.30

(Sets the date to November 25, 2021, at 18:00:30)
Set System Clock:

For systems with administrative rights, use the date command to set the system clock.

$ sudo date MMDDhhmm[[CC]YY][.ss]

Example:

$ sudo date 112518002021.30

Display Day of the Week:

Include the day of the week in the output.

$ date ‘+%A %B %d’

(Displays Tuesday October 15)
Display Seconds Since Epoch:

Show the number of seconds since the epoch (January 1, 1970).

$ date +%s

Display Time in 12-Hour Format:

Format the time to display in 12-hour format with AM/PM.

$ date ‘+%I:%M:%S %p’

(Displays 06:53:22 PM)
Display Time in Specific Timezone:

To view the date and time in a specific timezone, use the TZ environment variable.

$ TZ=America/New_York date

(Adjusts the timezone to Eastern Time)

FAQs for Date Command in Linux

1. What is the purpose of the date command in Linux?
– The date command in Linux is used to display the current date and time in various formats. It also allows users to customize the output, calculate dates, and set the system clock with administrative rights.2. Where can the date command be found?
– The date command is available on any Unix-like system. If you are using a Linux distribution, it includes the “date” tool.3. How do I use the date command to display the current date and time?
– Use the following simple command:
$ date

This will show the current date and time of the system.

4. Can I customize the date format displayed by the date command?
– Yes, you can specify a custom date format using the following syntax:

$ date +”%d-%m-%y”

Example output: 20-06-19

5. How do I specify a particular date using the date command?
– Use the “-d” flag followed by the desired date in the format “<year>-<month>-<day>”:

$ date -d “2015-6-20”

6. How can I output the date of the day before yesterday?
– Run the following command:

$ date –date=’2 days ago’

7. Is it possible to display the full month name and day of the month?
– Yes, use the following command:

$ date ‘+%B %d’

Example output: October 15

Further Reading:  Why We Use Top Command in Linux?

8. How do I set the system clock using the date command with admin rights?
– Execute the following command with sudo or as a superuser:

$ sudo date MMDDhhmm[[CC]YY][.ss]

Example:

$ sudo date 112518002021.30

9. Can I calculate a future date using the date command?
– Yes, specify the desired date in the MMDDhhmm[[CC]YY][.ss] format:

$ date 112518002021.30

10. How do I display the date and time in a specific timezone?
– Use the `TZ` environment variable to set the desired timezone:

$ TZ=America/New_York date

(Adjusts the timezone to Eastern Time)

Summary: Mastering the Date Command in Linux

In conclusion, this tutorial delves into the versatile functionality of the date command in Linux, transforming the mundane task of displaying time and date into a powerful tool for customization. From basic time presentation to complex date calculations and system clock adjustments, the date command empowers users to mold time to their needs.

Whether you are a seasoned Linux user or a beginner exploring the realms of Unix-like systems, understanding the nuances of the date command opens up a world of possibilities. By mastering this command, you gain not just the ability to view time but the means to manipulate it for your convenience. Elevate your Linux experience by incorporating the date command into your repertoire, and watch time bend to your command.