Search code examples
iosios7nsdatensdateformatternstimezone

Iphone date format


I was trying to format a time from GMT+7 to GMT+3:

I am building an app with a world clock in specific country (the user will be at the GMT+7and I want to represent the GMT+3 time )

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];    
[dateFormatter setLocale:[NSLocale currentLocale];    
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:118800];    
NSLocale *USLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];         
[dateFormatter setLocale:USLocale];    
NSLog(@"Date for locale %@: %@",
[[dateFormatter locale] localeIdentifier], [dateFormatter stringFromDate:date]);

I looked deep into NSDate class reference but I didn't understand how to make it.

Please if someone can help me I will be grateful.


Solution

  • There is 2 important parameters that works separately: Time and Time Zone.

    e.g: Vietnam uses GMT+7

    If I know that the time in Vietnam is 9:00 AM, then GMT time is 2:00 AM.

    When you get the Date from your device you are getting Time and Time Zone: YYYY-MM-DD HH:MM:SS ±HHMM. Where ±HHMM is a time zone offset in hours and minutes from GMT.

    Usually you are only using time. However with NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT"] you can tell the NSDateFormatter that you want the GMT time related to your local Time Zone. So, with:

    NSDateFormatter *dt = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
    [dt setTimeZone:timeZone];
    

    You can get the GMT date of your local time zone date.

    So, If you have GMT+7: 9:00 AM and you want to print out GMT+3: 5:00 AM, you have 3 possibilities:

    NSDate *localDate = [NSDate date];
    

    OPTION 1

    Add a time interval of -4 hours:

    NSTimeInterval secondsInFourHours = -4 * 60 * 60;
    NSDate *dateThreeHoursAhead = [localDate dateByAddingTimeInterval:secondsInFourHours];
    NSDateFormatter *dt = [[NSDateFormatter alloc] init];
    [dt setDateFormat:@"h:mm a"];
    NSLog(@"GMT+7(-4) = %@", [dt stringFromDate:dateThreeHoursAhead]);
    

    This is the easiest way to do it. If you are always at GMT+7 and you need GMT+3, this is a time interval of -4 hours.

    OPTION 2

    Set the time to GMT time zone and then add a +3hours time interval. The easiest way to do it is to add the 3 hours first and then move the time to GMT:

    NSTimeInterval secondsInThreeHours = 3 * 60 * 60;
    NSDate *dateThreeHoursAhead = [localDate dateByAddingTimeInterval:secondsInThreeHours];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
    [dateFormatter setTimeZone:timeZone];
    [dateFormatter setDateFormat:@"h:mm a"];
    NSString *date = [dateFormatter stringFromDate:dateThreeHoursAhead];
    NSLog(@"GMT+3 = %@", date);
    

    OPTION 3

    This is the better option. GMT+3 is EAT (East Africa Time) you can set your time zone to EAT with: [NSTimeZone timeZoneWithName:@"EAT"]

    NSDateFormatter *dt = [[NSDateFormatter alloc] init];
    [dt setDateFormat:@"h:mm a"];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"EAT"];
    [dt setTimeZone:timeZone];
    NSLog(@"EAT = %@", [dt stringFromDate:localDate]);
    

    Option 3 is always retrieving GMT+3

    An example code here.