Tajinder Tajinder - 4 months ago 8
iOS Question

How to get list of all UTC abbreviation in ios?

I have an app where i want to provide a list of UTC timezone so that the user could select destination time. I have all the country abbreviations in picker view. But i want UTC abbreviations.
Can anyone please suggest me how could i achieve this.



You can use the knownTimeZoneNames property of NSTimeZone for all timezones.

[NSTimeZone knownTimeZoneNames]

Or you can use abbreviationDictionary for getting all abbreviations

[[NSTimeZone abbreviationDictionary] allKeys]

If you want time of these timezones, the use the below code

NSArray *abbs = [[NSTimeZone abbreviationDictionary] allKeys];

    for (id eachObj in abbs) {

        NSString *dateStr = [self dateFromTimeZoneAbbreviation:eachObj];


define the method as

-(NSString *)dateFromTimeZoneAbbreviation:(NSString *)abb   {

    NSString *dateStr;

    NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
    NSTimeZone* timeZoneFromAbbreviation = [NSTimeZone timeZoneWithAbbreviation:abb];

    NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:[NSDate date]];
    NSInteger gmtOffset = [timeZoneFromAbbreviation secondsFromGMTForDate:[NSDate date]];
    NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;

    NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:[NSDate date]] ;

    NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
    [dateFormatters setDateFormat:@"dd-MMM-yyyy hh:mm"];
    /*[dateFormatters setDateStyle:NSDateFormatterShortStyle];
     [dateFormatters setTimeStyle:NSDateFormatterShortStyle];
     [dateFormatters setDoesRelativeDateFormatting:YES];*/
    [dateFormatters setTimeZone:[NSTimeZone systemTimeZone]];
    dateStr = [dateFormatters stringFromDate: destinationDate];
    NSLog(@"DateString : %@, TimeZone : %@", dateStr , timeZoneFromAbbreviation.abbreviation);

    return dateStr;