In short, the GMT date is returned unless otherwise specified. You can set the time zone to get the correct date. If you plan to use the date in the application to set something (for example, localNotification time or Event), you will need to do something special with the date, because if you set the date in iPhone, it will be set as GMT and will be off for a few hours. (in your case 4 hours). I am doing what I just described in one of my applications.
I made a mess trying to make it work correctly without turning off the clock. It was a huge PITA to sort out, but now it works. I copied, pasted, and edited my code for sharing. Again, its a mess, but it works! PickerChanged receives information from UIDatePicker
Using the code below. To answer your question, you can stop at "destinationDate". This will return you the corrected time for your current time zone. I just provided an extra fee when you tried to use the date on the phone somewhere.
NOTE. For a quick example, I put the event reminder in the same function as the datepicker, you DO NOT want to do this, otherwise you will have many reminders set every time the wheels scroll in the datepicker.
The code is below.
- (void)pickerChanged:(id)sender { NSLog(@"value: %@",[sender date]); NSDate* date= [sender date]; NSDateFormatter *formatter=[[[NSDateFormatter alloc]init]autorelease]; [formatter setDateFormat:@"MM/dd/yyyy hh:mm:ss a"]; [formatter setTimeZone:[NSTimeZone systemTimeZone]]; [formatter setTimeStyle:NSDateFormatterLongStyle]; NSString *dateSelected =[formatter stringFromDate:date]; NSString *timeZone = [dateSelected substringFromIndex:12]; NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
Louie
source share