Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Date Picker - TimeZone Issue

We are using a jQuery date picker on our website to select dates and times for bookings. The calendar is currently set at PST and this causes bugs when users try to access from other timezones. Should we set the server to be UTC and have the application automatically select user's timezone based on their IP Address. I was curious as whether we would also have to include manual selection as automatic selection may fail (i.e. when user works through proxy). Any advice would be greatly appreciated.

like image 454
Andrew Avatar asked Mar 22 '11 23:03

Andrew


2 Answers

The user already selects their timezone which is in their OS settings. Use javascript to detect this timezone (not based on IP), and pass it along with the date picker value to the server-side app. You can convert the local time to UTC time in Javascript or the server-side, whichever you prefer. But keep the server side app in UTC, since it's a good practice and internally that's what some languages uses, for eg., java.util.Date in Java.

like image 56
Vik David Avatar answered Oct 31 '22 19:10

Vik David


I would venture to guess that the front end doesn't need to have ANY understanding of timezones at all.

When they select a time from the datepicker, just pass it back as is (if they selected 9PM, just pass back 9PM). On the server, convert that date & time to a timestamp.

At that point, you can then make adjustments for the user's timezone based on IP, or however you choose to implement that. You can just add or subtract hours from your target time to match the user's timezone.

However, this means that you'll also need to re-parse the times when you output them to make sure that they show up how they should. So if you're storing a Unix timestamp, for example, make sure you print out a formatted date based on the location of the user.

You should definitely allow the user to choose their own timezone to override however you do it by default.

like image 27
rockerest Avatar answered Oct 31 '22 21:10

rockerest