user3573411
user3573411

Reputation: 63

DateTime conversion in iOS when device's timezone change

I get time in EDT from my server in format yyyy-MM-ddTHH:mm:ss for instance 2014-05-21T09:30:00. I convert these to NSDate and save in sqlite on iphone using code below

NSString *strDate = @"2014-05-21T09:30:00";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneWithName:@"EDT"]];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *date = [df dateFromString:strDate];

It works fine until I change the timezone on the device. If I change time on device time gets messed up. Any idea what am I doing wrong here?

  1. Device's timezone is Eastern and I save 8:00AM
  2. I change device's timezone to Pacific and time changes to 5:00AM
  3. I have an update process that updates all datetime values again and it goes back to 8AM
  4. I now change device's timezone to Eastern and time goes to 11AM
  5. I run the update process again and time goes back to 8AM

How can I save these times so no matter which timezone users is in, they always see eastern time.

Thanks,

D.

Upvotes: 0

Views: 182

Answers (1)

Doug Richardson
Doug Richardson

Reputation: 10831

NSDate itself doesn't know about timezone; it's just a simple wrapper around a double value, which represents the number of seconds since some fixed time (I think Jan 1 2001 or something) UTC.

What you need is a NSDateFormatter, similar to the one you use to parse the date, to transform the NSDate into an NSString using the EDT timezone.

Upvotes: 1

Related Questions