2

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.

Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
Roy
  • 105
  • 5
  • check this links, might help you: http://stackoverflow.com/questions/5985468/iphone-differences-among-time-zone-convenience-methods and http://agilewarrior.wordpress.com/2012/06/27/how-to-convert-nsdate-to-different-time-zones/ – Chitra Khatri Apr 23 '14 at 04:36

1 Answers1

3

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.

Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
  • first of all thanks for you help , I was forgot that in my country there is a summer clock and winter clock and this make it even more complex , there is an api i can add or something like this ? – Roy Apr 24 '14 at 07:54
  • As far as I can understand your local time have winter/summer clock and GMT+3 don't. I've got a similar app with Ethiopia Time (GMT+3), Your local time will upload automatically with this part of the NSDate: ±HHMM. And then you should just use OPTION 2: add 3 hours and move the NSDate to GMT. – Gabriel.Massana Apr 24 '14 at 09:05
  • Anyway, maybe OPTION 3 is working, too. You can test it changing the Date and Time Zone manually in your Mac. System Preferences / Date & Time / Date & Time or Time Zone – Gabriel.Massana Apr 24 '14 at 09:08