The date command is a powerful tool provided by the GNU Core Utilities package. The formatting options are incredibly useful when writing scripts. If you are unfamiliar with this tool read our primer The Linux Date Command - Display, Format & Set the Date on Linux. In this Linux quick tip we are going to use the TZ environmental variable to specify the timezone with the Linux date command. I use this often when I am working with servers that are geographically distant from me. For example, when I need a script to send me an email when an action completes. Having the date time sent to me using my timezone stops me from needing to do time conversions. It is also handy if you simply want to display the date and time in a different time zone.
Using TZ Variable to Specify Timezone for date Command
Here we are going to set the TZ variable before calling the date command. Here we will set the timezone to Africa/Douala.
[mcherisi@putor ~]$ date
Sat Apr 9 10:18:52 AM EDT 2022
[mcherisi@putor ~]$
[mcherisi@putor ~]$ TZ='Africa/Douala' date
Sat Apr 9 03:18:57 PM WAT 2022
As you can see putor workstation is set to EDT (Eastern Time). So when we use the date command it shows us the date in EDT. When we set the TZ variable to Africe/Douala and run the date command it shows us the time in WAT (West Africa Time).
Printing a List of Timezones
To set a timezone you need to know the exact formatting. This isn't always obvious. You can print a list of timezones using the timedatectl
command.
[mcherisi@putor ~]$ timedatectl list-timezones
There will provide you with a list of timezones. You can use the spacebar to advance to the next page. When you are done use CTRL+C to exit.
Conclusion
That's it and now you know how to specify the timezone with the Linux date command. Remember if you are using it in a script you may need to export the variable. Use the links and resources below to read more on date, timedatectl, and related subjects.
Links and Resources
Join Our Newsletter
Categories
- Bash Scripting (17)
- Basic Commands (50)
- Featured (7)
- Just for Fun (5)
- Linux Quick Tips (98)
- Linux Tutorials (65)
- Miscellaneous (15)
- Network Tools (6)
- Reviews (2)
- Security (32)