timeZoneWithAbbreviation:
Returns the time zone object identified by a given abbreviation.
+ (id)timeZoneWithAbbreviation:(NSString *)abbreviation
Parameters
- abbreviation
- An abbreviation for a time zone.
Return Value of [NSTimeZone timeZoneWithAbbreviation]
The time zone object identified by abbreviation determined by resolving the abbreviation to a name using the abbreviation dictionary and then returning the time zone for that name. Returns
nil
if there is no match for abbreviation.Discussion
In general, you are discouraged from using abbreviations except for unique instances such as “UTC” or “GMT”. Time Zone abbreviations are not standardized and so a given abbreviation may have multiple meanings—for example, “EST” refers to Eastern Time in both the United States and Australia
Example of [NSTimeZone timeZoneWithAbbreviation]
- (NSDate*) convertToUTC:(NSDate*)sourceDate
{
NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone* utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:sourceDate];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval gmtInterval = gmtOffset - currentGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:sourceDate] autorelease];
return destinationDate;
}
Example of [NSTimeZone timeZoneWithAbbreviation]
NSDate *cDate = [NSDate date];
NSLog(@"current date by NSDate %@",cDate); //but NSDate show GMT
2010-12-28 20:56:11.794 Done[484:307] current date by NSDate 2010-12-28 15:56:11 GMT
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm";
//// NSTimeZone *gmt = [NSTimeZone ]
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT+05:00"];
[dateFormatter setTimeZone:gmt];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@" date string object %@" ,timeStamp); // string From Date is GMT +5
2010-12-28 20:56:11.802 Done[484:307] date string object 2010-12-28T20:56
NSDate *datef = [dateFormatter dateFromString:timeStamp];
NSLog(@" date object %@" ,datef); // the date form above string gives again GMT
2010-12-28 20:56:11.809 Done[484:307] **date object 2010-12-28 15:56:00 GMT**
Example of [NSTimeZone timeZoneWithAbbreviation]
// The date in your source timezone (eg. EST)
NSDate* sourceDate = [NSDate date];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];