0

I currently get time as UTC and I want it to be convert into local time zone NSDate * startDate;

Example strDate - 14/9/2017 7.28 Am

Required format - 14/9/2017 1.52 pm

I need help with objective c.

NSDate * startDate ; //contain utc date

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, MMM d, yyyy - h:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
 NSString *timestamp = [dateFormatter stringFromDate:startDate];

but this is not correct time but date is correct

athira
  • 31
  • 5

2 Answers2

0

First convert NSString to NSDate.

 NSDateFormatter *DateFormatter = [[NSDateFormatter alloc] init];
 NSTimeZone *timeZoneEDT =[NSTimeZone timeZoneWithName:@"GMT"];
 [DateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
 [DateFormatter setTimeZone:timeZoneEDT];
 [DateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
 NSDate *CurrentDate = [DateFormatter dateFromString:DateStr];

Then convert GMT time to local time.

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:@"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
NSString *dateRepresentation = [dateFormatter stringFromDate:dateFromString];
phani
  • 105
  • 2
  • 15
0

@athira try this. it works for me.

    NSDate *startDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd/M/yyyy h.mm a"];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    NSString *timestamp = [dateFormatter stringFromDate:startDate];
    NSLog(@"date = %@",timestamp); //date = 14/9/2017 4.34 PM

it will print current time with proper GMT+5:30 and i have used localTimeZone.

Nirav Kotecha
  • 2,493
  • 1
  • 12
  • 26