Adding a world clock to our Whatsapp bot

Technology Mar 25, 2020

Another day, another idea. I travel a lot, sometimes to a different city every month and with that, comes the task of figuring out what the time back home is so I can talk to my family and my friends. My parents are in Bahrain, my sister is in India and I have a lot of friends and coworkers in the US and I am pretty tired of having to look up what the time is for them before giving them a call. More often than not, I use Whatsapp to make that call, so I thought, what if I could just see what time it is for them within Whatsapp itself?

Let's get started, shall we?

I am going to start by adding the moment-timezone library to our app. We will be using it to get the time in the countries we're after. We can then go ahead and create a list of locations that we will later use. Here's what my list looks like:

const locations = [
  {
    name: 'India',
    timezone: 'Asia/Kolkata',
  },
  {
    name: 'Bahrain',
    timezone: 'Asia/Bahrain',
  },
  {
    name: 'Miami',
    timezone: 'America/New_York',
  },
];

Once we add our locations, we can use them and the library we just added to get the time in those timezones whenever we want!

const time = moment();
const format = 'h:ma';
response = locations.map(location => {
    return `• The time in ${location.name} is ${time.tz(location.timezone).format(format)}`
}).join('\n');

All we have to do after this is use our time command to see the current time in all our mentioned locations.

Time command in action

That's all for this post. I think that was a very useful and very easy to add feature. Let me know if you found it useful as well and also if you have any other feature ideas that you'd like to see implemented!

Faraz Patankar

Hi! This is my website. The design looks better with the bio so I am adding it here. You can visit the about page to learn more about me!

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.