How can I fix the iOS objective-c timezone issue?

I am developing an SMS based application. When my application starts, I fetch the last time-date-time from the database and send it to the server, and I return all messages with date> last message-date-time, but since the application and the server may not be in the same time zone i can't get the correct result. What is the best way to solve this problem? Please help. Thank.

This question is a follow up to update SQLite with push notification content when app is down .

+3


source to share


3 answers


One option is to store the timestamp in UTC / GMT on the server, and when you get the date from the server, convert it to a local date object using GMT as the formate, and then show the user your local timezone using formatters, i.e.

  • Store the date in UTC / GMT on the server.
  • Get date string in app, use NSDateFormatter

    with GMT / UTC as timezone to convert it to object NSDate

    in Objective-C
  • Use NSDateFormatter

    with local timezone to show it to the user.


Keep in mind that NSDate keeps the date independent of timezones, so when showing it to the user, use any NSDateFormatter to convert it to the local or any timezone.

+2


source


Please convert the system timezone to your server's timezone first when you fetch the datetime from the database and then send it to the server.



+1


source


To figure out this problem. you need to follow these steps.

Step 1 . Always keep the server timezone in UTC.

Step 2 : as soon as you need to send the datetime to the server. you first need to convert it to UTC and then send it to the server for the server to store it.

Step 3 . When fetching the datetime from the server, you need to convert it to a respected timezone. where are you now. for example, if I am using an application from INDIA then I will add +5: 30 to the date I received from the server. therefore the whole country rules the same.

Hope this help will help you.

0


source







All Articles