How to handle timezone in SaaS Web Application


 When you are building a SaaS Web app, you are likely to have users from different timezone.

Handling time storing logic usually comes to your mind at the end. However, if not handled well, it can cause sleepless nights for you...Just kidding...


Here is the approach you need to follow the handle timezone logic.


1. On the Server side, initialize your server to work on the "UTC" timezone. So that you don't have to manually convert the time every time you create the Date & Time object.

In Spring MVC, It can be easily done by initializing it using Spring Configuration. 


@Configuration 

public class LocaleConfig { 

     @PostConstruct public void init() { 

             TimeZone.setDefault(TimeZone.getTimeZone("UTC")); 

 } 

}

 

I am sure all other servers would be having a similar mechanism to initialize timezone at the time of startup.


2. This will ensure the date & time stamp is always is stored and retrieved from database in UTC format.


3. Now on Front end, you can convert the UTC time to be displayed in the local timezone of the user's browser. This can be done using this simple javascript:


var dt = new Date('2021-07-24T20:37:26.007' + 'Z');

console.log(dt1.toLocaleString());


Hope this solves your timezone headache. It took me whole day of research to finally understand this simple logic.
Follow Me on Social Media

Advertisement
FREE Sales CRM Software

Fully customizable CRM Software for Freelancers and Small Businesses

Signup for Free

Sign up for DigitalOcean Cloud

Get FREE $200 credits, deploy your hobby projects for Free

DigitalOcean Referral Badge
Sign up for Hostinger Cloud or VPS plans and get upto 20% Discount