Updated July 3, 2023
Introduction to Linux Date
In the Linux ecosystem, the date prints or displays the current date and system time zone. With the help of the date command, we can print the date and time in a different format. The date commands provide the liberty to print past and future dates also. David MacKenzie wrote the date command utility.
Syntax of Date Command:
date [OPTION]... [+FORMAT]
- date: We can use the date keyword in the syntax or command. It will take the two-argument as an option and format type (to which we need to print the date). Provide the end result with the requested format date.
- OPTION: We can provide the different flags as options that are compatible with the date command.
- FORMAT: As per the requirement, we can pass the different types of date formats to the date command.
How does Linux Date Command work?
The date command can print different type date formats. By default, the date command will print the date as per the time zone configured in the UNIX or Linux operating system. We can also change the system time zone and print the different time zone dates. For changing the time zone, you should have the root privileges. Generally, the date command will take the two sets of arguments like option and date format. As per the inputs provided to it, it will manipulate the data and provide the date and time output in the request format.
Below is the list of formatting keywords which are commonly used with the date command:
- %a: It will display the weekday name example: Mon.
- %A: It will display the full weekday name example: Monday.
- %b: It will display the short month name example: Jan.
- %B: It will display the long month name example: January.
- %d: It will display the date of the month example: 01.
- %H: It will display the hour digit in 24hr format.
- %I: It will display the hour digit in 12hr format.
- %j: It will display the date of the year example: 001..366.
- %m: It will display the month in digit format example: 01..12.
- %M: It will display the minute value example: 00..59.
- %S: It will display the second value example: 00..60.
- %y: It will display the last two digits of the year example: 20.
- %Y: It will display the year example: 2020.
Examples of Linux Date
Given below are the examples mentioned:
It is a very simple and common date command in a Linux environment. It will print the default date format of the system.
- As per the above command, we are able to print the system time and date.
- It will print the day (in short format), month (in short format), date, time (include the hour, minute, and seconds), time zone (IST), and year (2020).
a. Date Command – Custom Format
The date command is very flexible, we can provide different custom inputs to the date command, and it will print the date format accordingly. We have the privilege to change the default date format to get the result in our own format.
date -d '21 Aug 1993' +'%A -> %d %B - %Y'
- As per the below screenshot, we have printed the custom date. As per the requirement, we can add different special characters also.
b. Date Command – String Format
In the date command, we have the functionality to pass the string parameters. Accordingly, the date command will print the output. It will not require following the date input format.
date -d "last month"
- We can add the string option with the date command. It is a quick way to display the dates.
Override the Time Zone
a. Date Command – With Different Time Zone
In the date command, we are able to display the time and date in different time zones.
- Using the “timedatectl list-timezones” command, we are able to find the list of time zones available in the Linux.
- As per the requirement, we can choose any time zone and pass the values to the TZ variable.
b. Date Command – Use Epoch Time Converter
The epoch timestamp is also called the UNIX timestamp. In this conversion, the date command is to use the epoch converter. It will calculate the number of seconds that have elapsed from January 1, 1970 at 00:00:00 UTC.
- We are able to calculate the time between the epoch timestamp and the current timestamp. The time difference will be in seconds.
c. Date command – With File Operation
In the Linux environment, we can use the date command differently. Here, we are able to find the last modification time of the file. We need to use the “-r” option with the command to get this timestamp.
date -r /etc/passwd
- As per the screenshot below, we can find the last modification time of the file “/etc/passwd”.
d. Date Command – Display Past Date
In the date command, we have the functionality to display the past date in different formats.
date --date="2 day ago"
- As per the above command, we are able to print the past 2 day’s date and time.
e. Date Command – Display Future Date
In the date command, we have the functionality to display the future date in different formats.
date --date="2 day"
- As per the above command, we are able to print the future 2 day’s date and time.
f. Date Command – Set System time and Date
With the help of the date command, we are able to set the required time in the system.
date --set="20200101 17:30"
- With the help of the date command, we are able to set the system time. We have set the manual time i.e. 01/01/2020 05:30 PM.
We have seen the uncut concept of “Linux Date Command” with the proper example, explanation, and command with different outputs. The date command displays the date and time in a different format. It will be widely used in shell and application jobs.
We hope that this EDUCBA information on “Linux Date” was beneficial to you. You can view EDUCBA’s recommended articles for more information.