Q:

Issues with dateformatter in iphone

I am getting a NSDate *newDate value as 2011-08-12 13:00:00 +0000.
Now when I try to convert it to a string by dateformatter as follows:
NSDateFormatter *dateFormatter= [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd h:mm:ss Z"];

NSString *datestr = [[dateFormatter stringFromDate:newDate] lowercaseString]; 

But the value which I get for datestr is 5:30 hours more than the newDate value. I don't know where I am wrong. I get value for datestr as 2011-08-12 18:30:00 + 5:30 (which is incorrect).

A:

It is basically converting the time to your local time zone. if you dont want that just set the time aone for date formatter:
[dateFormatter setTimeZone:[NSTimeZone <use any of the functions below to get your desired one>]];

Documentation link:
http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimeZone_Class/
Creating and Initializing Time Zone Objects
+ timeZoneWithAbbreviation:
+ timeZoneWithName:
+ timeZoneWithName:data:
+ timeZoneForSecondsFromGMT:
– initWithName:
– initWithName:data:
+ timeZoneDataVersion
Working with System Time Zones
+ localTimeZone
+ defaultTimeZone
+ setDefaultTimeZone:
+ resetSystemTimeZone
+ systemTimeZone

