Skip to main content

Command Palette

Search for a command to run...

Schedule a Job At The Same Time In Different Timezones

By using MongoDB you will be able to run cronJob at the same time for all users

Updated
1 min read
Schedule a Job At The Same Time In Different Timezones

The Story

Imagine you have an application and your users from different timezones and you need to send a message every day at 6:00 o'clock for each user to say Good Morning! ☕️, very simple right? let's see...

The First Try

The first and obvious solution is to run a cronjob 0 6 * * *

The code above will run every day at 6:00 and send a message to each user, which is what we need, right!? yes and no 😅

Yes, because only the users on the same server timezone will receive a message at the expected time.

No, because the users in the other timezone will receive a message at the wrong time.

How to fix this?

The idea popped into my mind after I saw this answer on StackOverflow, and because I'm using MongoDB in my project, I'll explain my approach to how I solved this issue.

The code above runs every half hour and queries all users who have time at 6:00 by using MongoDB date query.

Done 😉

btw, you should store the timezone of the user in the database.

More from this blog

Nureddin Badawi

7 posts

I build scalable, high-performance applications across frontend, backend, and cloud infrastructure. and I excel in modern React-based frontend ecosystems.