BASH Programming Utilities

Linux date Command

The date command is a simplistic command that, by default, shows the current date and time of the system. However, its functionality isn’t limited there. There are tons of ways of customizing the output, for example, presenting in various formats, calculating dates and even setting the system clock with admin rights!

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.

which date

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!

Date usage

The “date” tool uses the following command structure.

date <options> <format>

Run the “date” command in the terminal.

date

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.

Display formats

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.

date --iso-8601=seconds

date --iso-8601=minutes

date --iso-8601=hours

If you want the output in RFC 3339 format, use the following structure.

date --rfc-3339=seconds

date --rfc-3339=minutes

date --rfc-3339=ns

The same commands go for RFC 2822 format.

Format specifiers

If you want “date” to operate on a specific date, use the “-d” flag.

date -d "<year>-<month>-<date>"

“date” offers various display options. For example, you can define your own output!

date +"Week: %V; Year: %y"

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.

date +"Week: %V; Year: %Y"

The list of the format specifiers is too long to include. Here are a couple of interesting ones.

%d: Day of the month

%m: Month of the year

%Y: Year

%y: Last 2 digits of the year

%w: Day of the week

%A: Weekday name

%a: Weekday name (abbreviated)

%B: Full month name

%b: Abbreviated month name

%H: Hour (24-hour format)

%I: Hour (12-hour format)

%S: Second

%T: Time of the day

%d: Current date of the month

%x: Date representation of the current locale

%X: Time representation of the current locale

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.

date -r <file>

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=GMT+2 date

TZ also allows for different values. For example, regional notations are also allowed.

For all the available time zones, check the following directory.

cd /usr/share/zoneinfo && ls

It’s also possible to tell date to show various points in time.

# Show current time
date -d now

# Show today’s time
date -d today

# Yesterday
date -d yesterday

# Tomorrow
date -d tomorrow

# Time of Tuesday (current week)
date -d Tuesday

# Time of Tuesday (previous week)
time -d last-tuesday

# Next week
date -d next-week

# Previous week
date -d last-week

# Next month
date -d next-month

# Previous month
date -d last-month

# Next year
date -d next-year

# Previous year
date -d last-year

Calculating seconds

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.

date +%s

It’s also possible to calculate the seconds from epoch to provided date/time.

date -d "1975-01-31" +"%s"

How about converting epoch to a date? Just pass the epoch value to “date” and let it do its job.

date -d @123456789

Date can also determine the day of the week of the given date.

date -d "1999-01-31" +"%A"

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.

STARTTIME=`date`
echo $STARTTIME

Using the same method, it’s also possible to create a file with the date and time of creation imprinted as its name.

touch ~/Desktop/`date +%F`.txt

A similar method can also be used to create archives as well.

tar -czvf `date +%F`-google-chrome-backup.tar.gz ~/.config/google-chrome

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.

sudo date --set="<year><month><date> <time>"

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.

Final thoughts

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.

man date

info date

Enjoy!

About the author

Sidratul Muntaha

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.