Mitesh Dobareeya
Mitesh Dobareeya

Reputation: 1020

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];

NSLog(@"%@",outputString);

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.

Upvotes: 0

Views: 66

Answers (1)

vadian
vadian

Reputation: 285200

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";

Upvotes: 1

Related Questions