After Investing lot of time in solving the date-time difference problem between Server and Clients due to them being at different Timezone, I understood few things which I would share with you.
- You can’t control the clients timezone.
- You can only control your server side timezone.
- You can’t force a your client.to accept your specific date-time.
So there are two solution I came up with:
Solution 1: Ask for a preferred timezone from client
In this process you ask a preferred timezone to client and keep it stored in your system. While fetching data back to the client we make a wrapper which accepts the stored date-time with server timezone, converts to the clients preferred timezone and then send it to client.
Solution 2: Send the date-time with timezone to client
In this process when you send the date-time back to the client you attach the server timezone with it. At client side you can take help of some libraries like Moment.js, JQuery, etc. to convert to the clients system timezone automatically.
Now few question that you may also have like I had:
Q. What is the server timezone of stored date-time in database if solution 1 is applied?
Ans. Well I always prefer to use the UTC timezone while storing data. As it never creates a problem due to no Daylight Savings Time (DST) attached.
Q. When to use which solution?
Ans. Well its upon the data we are dealing with:
- Dealing with data that is going to be in used for many years from now better go for solution 1.
- Short term data to be used for an year or two go for solution 2.
Q. How to convert date-time in solution 1?
Ans. We can use some online services API from companies like google, microsoft, etc.
Else if you are using windows server for hosting your application you can use windows TimezoneInfo class in your c# application. For this you have to keep your auto update of time always on.
Q. Benefits of using online service or windows TimezoneInfo class?
Ans. Following are the Benefits:
- The date-time difference between server and client will be taken care automatically.
- Daylight Savings Time (DST) update based conversion will be accurate.
- Less Manual work in managing the conversion for different place on earth.
Q. Example for daylight savings time causing a problem in manual?
Ans. Well take this example:
Suppose that in a country “Apple” the day light savings was suppose to get off on 15th May in 2017 due to which you programmed in your application manually to do so. But on 13th May 2017 it was decided due to the change in the lunar phase that the date will be extended by 10 days. Now you have to make that change again else the system will start taking wrong date-time.
Now think this can happen with many countries at same or different time so all there data will need an update.
Here the service based or Windows TimezoneInfo class takes care of it automatically as they get updated by the service providers automatically putting less load on you.
(The above example is from live case that happened in real only the names and dates are changed here)
Now these were the 2 ways I handled the problem of date-time in my application.
Would surely like to hear from you how you took care of yours.