Hi,
I know if you google about this there are a lot of results that come up but unfortunately nothing seems to be working for me.
I am trying to make a simple app that among other things converts from one time zone (gained from a UIDatePicker) to another but it in some instances it returns a date that is a day off.
I tried doing things this way:
Code:
viewDidLoad {
datePicker.timeZone = [NSTimeZone timeZoneWithName:@"America/Los_Angeles"];
[super viewDidLoad];
}
- (IBAction)datePickerValueDidChange:(id)sender {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Sydney"];
NSLog(@"%@",[dateFormatter stringFromDate:datePicker.date]);
}
and this way:
Code:
- (IBAction)datePickerValueDidChange:(id)sender {
NSTimeZone* sourceTimeZone = datePicker.timeZone;
NSTimeZone* destinationTimeZone = NSTimeZone timeZoneWithName:@"Australia/Sydney"];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:datePicker.date];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:datePicker.date];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* date = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:datePicker.date] autorelease];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Sydney"];
NSLog(@"%@",[dateFormatter stringFromDate:date]);
But regardless of which method I use, if I input 7:01 am 7/16/11 (LA time) into the date picker for example it returns the date 12:01 am 7/16/11 (sydney time) when it should actually read 12:01 am 7/17/11 (one day later).
Im not sure if Im just doing something sill or what but has anyone got any ideas where I'm going wrong?