Search code examples
iosobjective-cnsdate

iOS: Convert any GMT to indian time format GMT + 5:30


I'm getting date string from server and its GMT+2 i think and unable to convert into indian GMT 5:30 format Here is the server date string 2017-01-11T05:08:15.157Z and i want this in indian time format i have tried below code but getting null

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.Z"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSDate *date = [dateFormatter dateFromString:stmp]; // create date from string

[dateFormatter setDateFormat:@"dd-MM-yyyy - h:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:date];
NSLog(@"New Date%@", timestamp);

How to convert this into 11-01-2017 08:14 PM format. Thanks in advance.


Solution

  • Its Done by making small change i.e instead of giving date format yyyy-MM-dd'T'HH:mm:ss.Z changede to yyyy-MM-dd'T'HH:mm:ss.zzzZ its working fine here is the code i have changed We can change any GMT to system local time zone Thank you

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.zzzZ"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    NSDate *date = [dateFormatter dateFromString:stmp]; // create date from string
    
    [dateFormatter setDateFormat:@"dd-MM-yyyy h:mm a"];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    NSString *timestamp = [dateFormatter stringFromDate:date];
    NSLog(@"New Date%@", timestamp);
    

    here is the output New Date23-12-2016 4:47 PM