This tool is available on any Unix-like system. If you’re running any sort of Linux distro, it already features the “date” tool. Here’s how to ensure that your system already has it.
I’m using Manjaro Linux as my main driver and “date” tool is already present on the system. Whenever running “date”, the tool will be called from “/usr/bin” directory.
Let’s check out the usage of this simplistic tool!
The “date” tool uses the following command structure.
Run the “date” command in the terminal.
The output is pretty understandable. It’s Friday, July 26, 2019, and the command was run on 11:13:01 PM (12-hour format) and the time zone is GMT +6.
The “date” tool uses the time/date format of the current locale, right? However, there are a lot of display formats used worldwide. If you want the time output in that format, “date” is capable of doing so.
For example, the following command will print output in ISO 8601 format.
If you want the output in RFC 3339 format, use the following structure.
The same commands go for RFC 2822 format.
If you want “date” to operate on a specific date, use the “-d” flag.
“date” offers various display options. For example, you can define your own output!
In this command, the “%V” shows the current week number and the “%y” shows the last 2 digits of the current year. For the full year length, use “%Y” formatter.
The list of the format specifiers is too long to include. Here are a couple of interesting ones.
Check file modification time
“date” is incredibly versatile. This tool can also be used to check the time when a certain file/folder was last modified.
Overriding the time zone
By default, whenever running date, it will use the current time zone. The time zone is set in the environment variable TZ. The default value of TZ is defined in the file “/etc/localtime”.
By changing the value of TZ, we can force date to show time of different time zones.
TZ also allows for different values. For example, regional notations are also allowed.
For all the available time zones, check the following directory.
It’s also possible to tell date to show various points in time.
date -d now
date -d today
date -d yesterday
date -d tomorrow
date -d Tuesday
time -d last-tuesday
date -d next-week
date -d last-week
date -d next-month
date -d last-month
date -d next-year
date -d last-year
The “date” tool also includes this interesting feature where it can count the number of seconds from the UNIX epoch time (00:00:00, Jan 1, 1970) to the current time.
It’s also possible to calculate the seconds from epoch to provided date/time.
How about converting epoch to a date? Just pass the epoch value to “date” and let it do its job.
Date can also determine the day of the week of the given date.
Usage in scripts
With all these tricks, you can now implement the abilities into your scripting as well. Here, I’ve shown just a couple of ways of using “date” in your scripts.
Using the same method, it’s also possible to create a file with the date and time of creation imprinted as its name.
A similar method can also be used to create archives as well.
Changing date and time
“date” can also be used to change the system’s date and time. If your system uses ntpd and configured time zone correctly, then you shouldn’t have to go through this step. However, if there’s the need, using the “–set” flag will do the job.
For using this flag, the command follows this structure.
Note: The time should be in 24-hour format. This operation requires admin right, so make sure that you get permission from the system admin.
As you can see, the current system’s time and date is completely changed. Just make sure that you’re not setting a really wrong time.
This “date” tool is available on all the UNIX-based systems. With so many powerful features, “date” can be integrated into lots of cases. If you’re in confusion or interested in mastering “date”, then the man and info pages are the most helpful.