Friday, May 10, 2013

NSDateFormatter setTimeZone example ios



setTimeZone:

Sets the time zone for the receiver.
- (void)setTimeZone:(NSTimeZone *)tz
Parameters
tz
The time zone for the receiver.

Example of [NSDateFormatter setTimeZone]

NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
  NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
  [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  [dateFormatter setTimeZone:gmt];
  NSString* dateString = [dateFormatter stringFromDate:[NSDate date]];
  NSLog(@"Date/Time is %@", dateString);

  NSDateFormatter *inputFormatter = [[[NSDateFormatter alloc] init] autorelease];
  [inputFormatter setTimeZone:gmt];
  [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];    
  NSDate* date = [inputFormatter dateFromString:dateString];        
  NSLog(@"Date/Time is %@", date);

Example of [NSDateFormatter setTimeZone]
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss z"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [NSDate date];
NSString *dateString = [dateFormatter stringFromDate:date];
Example of [NSDateFormatter setTimeZone]
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm";

    NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    [dateFormatter setTimeZone:gmt];
    NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
    [dateFormatter release];