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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With