The date command is a command-line utility for displaying or setting date and time in the Linux system. It uses the system default time zone to display the time.
In this article, I will show you 12 examples of how to best use the date command on Linux. To demonstrate the examples below I have used an Ubuntu 20.04 system. As the date command is pre-integrated in all Linux systems we don’t need to install it.
Syntax :
$ date [OPTION]... [+FORMAT]
By default, the date command will display the current system date and time in a default format.
$ date
Current date of the system.
If your system time zone is based on your local time zone and you want to check the universal time, to do so we need to add the -u option to the command which refers to UTC.
$ date -u
UTC.
We can overwrite the default date format with our preferred date format. To achieve that we need to add a format control character led by + sign and format control begins with the % sign. Some of the most used date format control characters are:
Here, in the following example, we formatted the date in yyyy-MM-dd format.
$ date +"%Y-%m-%d"
Formatting date.
Similarly,
$ date +"%d %b %Y"
Formatting date.
We can display the formatted date from the date string provided by the user using the -d or --date option to the command. It will not affect the system date, it only parses the requested date from the string. For example,
$ date -d "Feb 14 1999"
Parsing string to date.
$ date --date="09/10/1960"
Parsing string to date.
Aside from parsing the date, we can also display the upcoming date using the -d option with the command. The date command is compatible with words that refer to time or date values such as next Sun, last Friday, tomorrow, yesterday, etc. For examples,
$ date -d "next Mon"
Displaying upcoming date.
Using the -d option to the command we can also know or view past date. For examples,
$ date -d "last Fri"
Displaying past date
If you have a record of the static date strings in the file we can parse them in the preferred date format using the -f option with the date command. In this way, you can format multiple dates using the command. In the following example, I have created the file that contains the list of date strings and parsed it with the command.
$ date -f datefile.txt
Parse date from the file.
We can not only view the date but also set the system date according to your preference. For this, you need a user with Sudo access and you can execute the command in the following way.
$ sudo date -s "Sun 30 May 2021 07:35:06 PM PDT"
We can check the file’s last modification time using the date command, for this we need to add the -r option to the command. It helps in tracking files when it was last modified. For example,
$ date -r /etc/hosts
Last modified date.
The date command will display the date according to your configured system time zone. We need to set the TZ variable to the desired time zone to use various time zones in the environment. For example, to switch to New York time, execute:
$ TZ='America/New_York' date
Date with prefer time zone
To see all available time zones, use the timedatectl list-timezones command.
Epoch time is the number of seconds that have passed since January 1, 1970, at 00:00:00 UTC. We can use %s format control to view the number of seconds from epoch time to current time.
$ date +%s
Unix epoch time.
We can create files with the current date which helps in keeping the track record of the file. In the following example, I have created a file including a current date in its name.
$ touch demo-$(date +"%Y-%m-%d”)
Naming file with the date.
In this article, we learn how to use the date command and how to pare send format dates on Linux.
Magento is a free and open-source e-commerce platform written in PHP. It is simple, easy…
ISPConfig is an open-source control panel that allows users to manage multiple servers from a…
As a Linux administrator, you may find it necessary to troubleshoot or test your Simple…
Ubuntu 24.04, like many modern Linux distributions, relies on the NetworkManager for managing network connections.…
Restic is a modern, open-source backup program designed for efficiency, security, and simplicity. It enables…
phpMyAdmin is a popular free tool written in PHP intended to administer MySQL and MariaDB…