Date Command in Linux Terminal with examples

Date Command in Linux Terminal

In this article we are going to shows how to use date command in Linux Terminal. If you want to learn about date command in Linux with Examples then this post is ideal for you.

In the Linux, the date is a command line utility that is used to get or set the system date. It is also used to display the date and time in different kind of formats and calculate the future and past dates.

Linux is an operating system, like Windows OS, iOS, and Mac OS. It is one of the most popular operating system on the planet, Android is also powered by Linux OS(operating system).

Linux OS(operating system) is also distributed under an open source license. Open source following these points :

  • Freely to run the program, for any purpose.
  • Free to study how the program works.
  • Freely to re-distribute copies so you can help your friends.
  • Free to distribute copies of your modified versions to others.

How to use date Command in Linux


date Command Syntax in Linux

In the Linux, the basic syntax of the date command is show as below:

date [OPTION]... [+FORMAT]

When you run the date command without any options then it will diplay the current system time and date in default format.

$ date
Output
Sun Jan 24 10:25:45 UTC 2021

Date Formatting Options 

Run the below command to display the output of date command in a specific sequence of format :

$ date +"Year: %Y, Month: %m, Day: %d"
Output
Year: 2021, Month: 01, Day: 24

Another example:

$ date "+DATE: %D%nTIME: %T"
Output
DATE: 24/01/21
TIME: 10:27:22

To check the full list of all available formatting options, simply run date --help or man date  command in your terminal.

$ date --help
$ man date

Date String 

Use -d option to operate on a specific date, simply run the below command to do that :

$ date -d "2021-01-24 10:30:33"
Output
Sun Jan 24 10:30:33 UTC 20201

Custom formatting:

$ date -d '24 Jan 2021' +'%A, %d %B %Y'
Output
Sunday, 24 January 2021

The date string also accepts values like “tomorrow”, “Friday”, “last Friday”, “next month”, “next week”, “yesterday”, “1 year”, “2 day” etc.

$ date -d "last Sunday"
Output
Sun Jan 17 00:00:00 UTC 2021

Override the Time zone

To use a different timezone set the environment variable TZ to the desired timezone.

For an example:

TZ='America/New_York' date
Output
Sun Jan 24 13:17:52 UTC 2021

You can display the list of all available time zones using below command :

$ timedatectl list-timezones

To print the Last Modification Time of a File

To get the last modification time of a given file, simply use the -r option along with the date command as below :

$ date -r /etc/passwd
Output
Sun Jan 24 13:26:00 UTC 2021

That’s all

If you face any error and issue in above steps , please use comment box below to report.

If our tutorials helped you, please consider buying us a coffee. We appreciate your support!

Thank you for your support.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top