How to Use Custom Timezone in Date CLI Command

April 07, 2022

At work, we’ve started using Calendar Versioning for one of our projects.

We’ve decided to use the following pattern for the version → YYYY.MM.DD-HH.MM (UTC by default)

currentDate=`date +"%Y.%m.%d-%H.%M"`
echo $currentDate

But as soon as we’ve started using it, we’ve noticed that it’s hard (mentally) to differentiate old and new version based on UTC.

And so we’ve configured it to use a Indian Standard Timezone(IST).

Solution

Here’s how we’ve done it:

currentDate=TZ=':Asia/Calcutta' date +"%Y.%m.%d-%H.%M"
echo $currentDate

And you can refer to list of tz database time zones to figure out your TZ value.


👋 I'm Ashik Nesin • Sr. Frontend Engineer @Chargebee 👨‍💻• I write about modern Javascript & building web apps

© 2022 Nesin Technologies LLP