Introduction to Linux Date
In the Linux ecosystem, the date is used to print or display the current date and system time zone. With the help of date command, we can print the date and time in a different format. The date commands provide the liberty to print the past and future dates also. The date command utility was written by David MacKenzie.
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 format to the date command.
How Linux Date Command works?
The date command is able to print different type date format. By default, the date command will print the date as per the time zone which is configured in the UNIX or Linux operating system. We can also change the system time zone and print the different time zone date. For changing the time zone, you should have the root privileges. Generally, the date command will take the two set 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 is commonly use with 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 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 digit of year example: 20.
- %Y: It will display the year example: 2020.
Examples of Linux Date
Given below are the examples mentioned:
It is 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, seconds), time zone (IST), year (2020).
a. Date Command – Custom Format
The date command is very flexible, we can provide the different custom inputs to the date command and it will print the date format accordingly. We are having the privileges to change the default date format to get the result in own format.
date -d '21 Aug 1993' +'%A -> %d %B - %Y'
- As per the below screenshot, we have print the custom date. As per the requirement, we can add the different special character also.
b. Date Command – String Format
In date command, we are having the functionality to pass the string parameters. Accordingly, the date command will print the output. It will not require to follow 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 date command, we are able to display the time and date in different time zones.
- Using “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 as 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 in 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 in a different way. Here, we are able to find the last modification time of the file. To get this timestamp, we need to use the “-r” option with the command.
date -r /etc/passwd
- As per the below screenshot, we are able to find the last modification time of the file “/etc/passwd”.
d. Date Command – Display Past Date
In date command, we are having 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 date command, we are having 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 date command, we are able to set the required time in the system.
date --set="20200101 17:30"
- With the help of 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 is used to display the date and time in a different format. It will be widely used in shell and application jobs.
This is a guide to Linux Date. Here we discuss how linux date command works along with examples respectively. You may also have a look at the following articles to learn more –