Mitesh Dobareeya Mitesh Dobareeya -4 years ago 187
Objective-C Question

Unable to convert UTC time to local time?

I am getting UTC time string from server as "8/27/2016 6:56:23 AM"

I need to convert this date to my local time zone.

NSString *dateFormat = @"dd/MM/yyyy HH:mm:ss a";
NSTimeZone *inputTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
[inputDateFormatter setTimeZone:inputTimeZone];
[inputDateFormatter setDateFormat:dateFormat];

**NSDate *date = [inputDateFormatter dateFromString:_strLiveStreamStartTime];**

NSTimeZone *outputTimeZone = [NSTimeZone localTimeZone];
NSDateFormatter *outputDateFormatter = [[NSDateFormatter alloc] init];
[outputDateFormatter setTimeZone:outputTimeZone];
[outputDateFormatter setDateFormat:dateFormat];
NSString *outputString = [outputDateFormatter stringFromDate:date];


The problem is when i covert server string into date it gives me null value.
Please help me to solve this. I google it and implement some solution but unable to get.

Answer Source

Your string is month/day..., your date format is day/month..., that's the issue. And the hour is supposed to be represented by lowercase hh for 12 hour mode.

NSString *dateFormat = @"MM/dd/yyyy hh:mm:ss a";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download