Search code examples
objective-cnsdatensdateformatternstimezone

objective-c: Conversion between TimeZones


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


Solution

  • 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