Friday, May 3, 2013

NSTimeZone timeZoneWithName example ios


timeZoneWithName:

Returns the time zone object identified by a given ID.
+ (id)timeZoneWithName:(NSString *)tzName
Parameters
tzName
The ID for the time zone.
Return Value
The time zone in the information directory with a name matching tzName. Returns nil if there is no match for the name.
Example of [NSTimeZone timeZoneWithName]
-(NSString *)getUTCFormateDate:(NSDate *)localDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [dateFormatter setTimeZone:timeZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:localDate];
    [dateFormatter release];
    return dateString;
}
Example of [NSTimeZone timeZoneWithName]
NSDate *now = [NSDate date];
 NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
 [df setTimeStyle:NSDateFormatterLongStyle];
 [df setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Melbourne"]];
 NSString *melbourneTime = [df stringFromDate:now];
 [df setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Berlin"]];
 NSString *berlinTime = [df stringFromDate:now];
 [df setTimeZone:[NSTimeZone timeZoneWithName:@"America/Los_Angeles"]];
 NSString *laTime = [df stringFromDate:now];
 NSLog(@"%@", melbourneTime);
 NSLog(@"%@", berlinTime);
 NSLog(@"%@", laTime);