Friday, May 3, 2013

NSTimeZone initWithName example ios


initWithName:

Returns a time zone initialized with a given ID.
- (id)initWithName:(NSString *)tzName
Parameters
tzName
The ID for the time zone. Must not be nil.
Return Value of [NSTimeZone initWithName]
A time zone object initialized with the ID tzName.
Discussion
If tzName is a known ID, this method calls initWithName:data: with the appropriate data object.
In OS X v10.4 and earlier providing nil for the parameter would have caused a crash. In OS X v10.5 and later, this now raises an invalid argument exception.
Example of [NSTimeZone initWithName]
NSString *tzStr = @"GMT-09:00";
NSTimeZone *tz = [[NSTimeZone alloc] initWithName:tzStr];
NSLog(@"Tz: %@", tz);
NSLog(@"Offset: %d", [tz secondsFromGMT]);

// Tz: GMT-0900 (GMT-09:00) offset -32400
// Offset: -32400
Example of [NSTimeZone initWithName]
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"HH:mm dd MMM yyyy";
dateFormatter.timeZone = [[NSTimeZone alloc] initWithName:@"GMT"];

NSLog(@"%@", [dateFormatter dateFromString:@"00:00 15 September 2012"]);