I am working on an application (using the spring framework) in which current time is stored in the db whenever new records are added.
So when these records are shown to the user in the frontend, the time shown is as per the server’s timezone and not the client’s. As the application targets global audience, it might get confusing to the users if they see a record they just added with a different time.
I am guessing that this problem must be faced by all apps that have users located globally.
What would be the best way to deal with this issue ?
Two options I am considering after doing some searching and reading a few answers on SO are –
1) Getting the user’s timezone using javascript or from the ip adress and storing it in the db instead of the server time.
2) Showing the user, time relative to current time for records recently added (for eg. added 2 mins ago upto added 1 day ago and for earlier records show the actual date.
Not considering the first one as I feel it will result in an inconsistency between the primary key and the created at column in the tables. May not be harmful currently as the users only see their own records but will lead to confusion if each others records are to be shown to them in future.
The second method looks fine to me. In fact, I can see similar stuff on SO (for eg in newest questions relative time is shown and featured questions that are added a few days back show ‘actual’ date and time) but not sure if the same issue is being addressed here.
Would like to know any other approaches.
Do #2 as long as you can get away with it. This can be used as long as you have a timestamp associated with each piece of data. If you need to show an actual date (on a calendar for instance), then you must do #1 (or something similar).
Javascript uses the computers time, so you can always convert server times to local times using Javascript as long as you know the server timezone. If for whatever reason you need to store data in the database in the user’s timezone, always store the timezone along with the time; otherwise it’s meaningless. Perhaps a better option in this case is to store the times in server time and store the user’s timezone somewhere else, then convert times when you need to (as late as possible).