I have a client server model where server will send some data which has date parameters and iPhone will need to render it. Server and iPhone will resides in different timezones. So I need to synchronize times in both. Let say user has done some activity at "07/18/2011/04 /45/EDT" (server time). From the iPhone end I need to convert this date string into a NSDate object.
What is the best way to achieve this.
Thank you
You can definitely achieve this in following way:
1) When you fetch data from server first time, then pass the date value as "" (blank). So that, server will identify that, user require all data.
2) In above service, please add "Date" tag in server web-service. So, when you get other data, you can also get the server date. So, store this server date in local.
3) Next time, when you get other data then pass this stored date (which will be in server format and timezone), so that server will give you only those data which are newly added or modified after this data.
4) This way you can handle the date problem. And also there will be no issue with local time or time zone.
Please review above point and let me know in case of any query of difficulty.
I have implement this in most of my application and it's working fine.
For UTC Date, you can use below function:
I hope, it will be helpful to you.
Thanks.
Sorry for the late response, just looked for "NSDate Sync to Server" on google and got here. I got stuck with the same issue and written this nice little category that helps.
It can return an NSDate object synced to any web server with only a single HTTP Request (per-session).
https://github.com/freak4pc/NSDate-ServerDate
If you have any questions feel free to let me know.
If you want time to be the same on server and client, synchronize both with NTP (network time protocol). There is an obj-c ntp client available at http://code.google.com/p/ios-ntp/source/browse/trunk/ntpA.xcodeproj/project.pbxproj?spec=svn25&r=25
As suggested above, aways store / send times as unix timestamps (seconds since the epoch UTC) and convert to timezones as necessary.