Feroz Feroz - 1 year ago 182
iOS Question

Convert GMT to IST in iOS

How do I convert GMT to IST in IOS ? I have code which will give me time in GMT. But I want to convert it into the corresponding local time zone and use it in my apps. How can I do this ?
Thanks in Advance.

Answer Source

The code below convert GMT to IST.

NSString *inDateStr = @"2000/01/02 03:04:05";
NSString *s = @"yyyy/MM/dd HH:mm:ss";

// about input date(GMT)
NSDateFormatter *inDateFormatter = [[NSDateFormatter alloc] init];
inDateFormatter.dateFormat = s;
inDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSDate *inDate = [inDateFormatter dateFromString:inDateStr];

// about output date(IST)
NSDateFormatter *outDateFormatter = [[NSDateFormatter alloc] init];
outDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"IST"];
outDateFormatter.dateFormat = s;
NSString *outDateStr = [outDateFormatter stringFromDate:inDate];

// final output
NSLog(@"[in]%@ -> [out]%@", inDateStr, outDateStr);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download