I'm having problems converting a time string to a different timeZone.
This is a string example as the one I retrieve from a server: @"5/14/2013 4:19am"
I know the timeZone is America/New_York and I want to convert it to my local timeZone: Europe/Rome.
Here is the code I wrote to test it:
-(void) convertTimeZone
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone=[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
dateFormatter.dateFormat=@"MM/dd/yyyy HH:mma";
NSString *sourceDateString = @"5/14/2013 4:19am";
NSTimeZone *sourceTimeZone = [[NSTimeZone alloc] initWithName:@"America/New_York"];
NSTimeZone *destinationTimeZone = [NSTimeZone localTimeZone];
NSInteger sourceGMToffset = sourceTimeZone.secondsFromGMT;
NSInteger destinationGMToffset = destinationTimeZone.secondsFromGMT;
NSInteger interval = destinationGMToffset-sourceGMToffset;
NSLog(@"DateFormatter TimeZone: %@", dateFormatter.timeZone);
NSLog(@"Source TimeZone: %@", sourceTimeZone);
NSLog(@"Destination TimeZone: %@", destinationTimeZone);
NSLog(@"sourceOffset: %u destinationOffset: %u interval: %u", sourceGMToffset, destinationGMToffset, interval);
NSLog(@"----------------------------------------------------------------------------------------------------");
NSLog(@"sourceDateString : %@", sourceDateString);
//Convert from string to date
NSDate *dateObject = [dateFormatter dateFromString:sourceDateString];
NSLog(@"From string to date: %@", dateObject);
//Now back from date to string
NSLog(@"From date to string: %@", [dateFormatter stringFromDate:dateObject]);
//Convert from sourceTimeZone to destinationTimeZone
NSDate *newDate = [[NSDate alloc] initWithTimeInterval: interval sinceDate:dateObject];
NSLog(@"destinationDateString: %@", [dateFormatter stringFromDate: newDate]);
}
output:
DateFormatter TimeZone: GMT (GMT) offset 0
Source TimeZone: America/New_York (GMT-04:00) offset -14400 (Daylight)
Destination TimeZone: Local Time Zone (Europe/Rome (CEST) offset 7200 (Daylight))
sourceOffset: 4294952896 destinationOffset: 7200 interval: 21600
sourceDateString : 5/14/2013 4:19am
From string to date: 2013-05-14 00:19:00 +0000
From date to string: 05/14/2013 00:19AM
destinationDateString: 05/14/2013 06:19AM
I'm getting mad!!!!
If the original date is "5/14/2013 4:19am" WHY the date formatter changes it to "05/14/2013 00:19AM" ????????? If it had saved correctly it to "05/14/2013 04:19AM +0000" the conversion would be perfect (6 hours between the two TimeZones)!!!
Any hint?
Thanks
Nicola
EDIT
By setting these parameters as suggested by lithu-t-v:
dateFormatter.timeZone=[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSString *sourceDateString = @"5/14/2013 4:19am -0400";
It seems to work but it doesn't.
I say "it seems" because:
sourceDateString: 5/14/2013 04:19am -0400
From string to date: 2013-05-13 04:19:00 +0000 (OK)
From date to string: 05/14/2013 04:19 (OK)
destinationDateString: 05/14/2013 10:19 (CORRECT)
is correct BUT if I try with a late pm hour:
sourceDateString: 5/14/2013 10:19pm -0400
From string to date: 2013-05-14 16:19:00 +0000 (WRONG)
From date to string: 05/14/2013 16:19
destinationDateString: 05/14/2013 22:19 (WRONG!)
The correct result should be: 05/15/2013 04:19AM
Thank you all for all your hints! After a lot of trials I ended writing a function that uses DateComponent:
It returns the right output:
Original Date Time: 5/13/2013 04:19am (I know it is America/New_York)
GMT Date Time: 2013-05-13 08:19:00 +0000 (OK)
Output: 13/05/13 10:19:00 CEST
This is what you want:
Output:
The first mistake you made was in the date format specification
HH
is the time in 24 hour format. When used with the am/pm specifier, this seems to always parse the hour as 0. Your test data coincidentally had an hour of 4 which is the same as the offset between GMT and New York, leading you to assume it was just getting the offset wrong. I changed the hour specifier toh
to get 12 hour time. I also changed the test time, to make it less confusing.The second mistake was to mess about with the NSDate object, that way lies insanity. NSDates are always in GMT. Do not add or subtract time intervals to correct for time zones, just set the time zone in the date formatter appropriately. So my code sets the time zone to New York and parses the date. It then sets the time zone to local time (I'm in the UK on BST) and stringifies the date. Job done.
The +0000 component defines the date with the time zone and provide with actual time .A NSDate object include this part also.since that part is missing the time difference comes,with respect to local time zone
Here you ceated the timezone but is not set to the datefromatter.Set timezone to the dateformatter and try it
Append +0000 to the string and do the remaining.It will work