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 11 examples of how to best use the date command on Linux. To demonstrate the examples below I have used an Ubuntu 21.04 system. As the date command is pre-integrated in all Linux systems we don’t need to install it.
$ date [OPTION]… [+FORMAT]
Date Command Examples
By default, the date command will display the current system date and time in a default format.
Current date of the system.
Displaying Universal Time
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
Custom Date Format
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:
- %a – Locale’s abbreviated short weekday name (e.g., Wed)
- %A – Locale’s abbreviated full weekday name (e.g., Wednesday)
- %b – Locale’s abbreviated short month name (e.g., Jun)
- %B – Locale’s abbreviated long month name (e.g., June)
- %Y – Display Year (e.g., 2021)
- %m – Display Month (01-12)
- %d – Day of month (e.g., 02)
- %D – Display date as mm/dd/yy
- %H – Hour in 24 hr format (00-23)
- %I – Hour in 12 hr format (01-12)
- %M – Display Minute (00-59)
- %S – Display Second (00-60)
- %u – Day of the week (1-7)
Here, in the following example, we formatted the date in yyyy-MM-dd format.
Displaying Date From String
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,
Parsing string to date.
Parsing string to date.
Displaying Upcoming Date & Time With -d Option
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,
Displaying Next Monday Date
$ date -d “next Mon”
Displaying upcoming date.
Displaying Past Date & Time With -d Option
Using the -d option to the command we can also know or view past date. For examples,
Displaying Last Friday Date
Displaying past date
Parse Date From File
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.
Setting Date & Time on Linux
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.
Display File Last Modification Time
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.
Override the System Timezone
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:
Date with prefer time zone
To see all available time zones, use the timedatectl list-timezones command.
Use Unix Epoch Time
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.
Using Date in File Naming
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.
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..
- Another article that you can be interested in is 4 Social Media Command Line Tools (Facebook,Twitter,Reddit).
- Or 6 Best Password Managers For Linux Desktop ( for 2021 )
- And 10 monitoring tools for Linux system administrators you should know about