0

It looks easy, but I couldn't figure out a proper way to do this. I need to create an NSString from a NSDate which represents the same time on every device, independently from the iPhone's time zone settings.

Suppose userA is in London, where the actual time is 14:00, userB is in New York where is 9:00 and userC is in Hong Kong, where the actual time is 21:00.

NSDate *now = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yy HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:now];

Actually with this code I'm getting these results (when I log the dateString):

  • userA: 08/12/14 14:00:00
  • userB: 08/12/14 09:00:00
  • userC: 08/12/14 21:00:00

But I need to create dates like this

  • userA: 08/12/14 14:00:00
  • userB: 08/12/14 14:00:00
  • userC: 08/12/14 14:00:00

My goal is to create a "system/absolute time" which is the same inside the app and doesn't matter the original time zone of the user's device. The end result must look like this MM/dd/yy HH:mm:s.

Is it possible to get the NSDate *now = [[NSDate alloc] init]; from a pre-defined timezone? For example it could always use the actual time of the GMT-00 timezone.

I've tried to do it with this code, but when I run the code, the console writes out the wrong date (based on the device time zone setting) again, so I don't have a better idea. I would appreciate any ideas.

NSDate *now = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

dateFormatter.timeZone  = [NSTimeZone timeZoneWithAbbreviation:@"Europe/London"];
[dateFormatter setDateFormat:@"MM/dd/yy HH:mm:ss"];

NSString *dateString = [dateFormatter stringFromDate:now];
NSLog(@"the date is %@,", dateString);
rihekopo
  • 3,241
  • 4
  • 34
  • 63
  • Because it's not an abbreviation what you are feeding there. Other than that you are perfectly on the right track. – Desdenova Aug 14 '14 at 13:37
  • @Desdenova What do you mean exactly? What should I do different? – rihekopo Aug 14 '14 at 13:43
  • 1
    `Europe/London` is not an abbreviation. It's probably returning `nil` and the device gets the time zone from the system. `GMT` is an abbreviation for example. – Desdenova Aug 14 '14 at 13:46
  • possible duplicate of [GMT timezone conversion in objective c](http://stackoverflow.com/questions/8871727/gmt-timezone-conversion-in-objective-c) – Rog Aug 14 '14 at 14:02

3 Answers3

2

The below code should work. What ever the timezone you are in it will always display the time in UTC.

NSDate *now = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[[NSTimeZone alloc] initWithName:@"UTC"]];
[dateFormatter setDateFormat:@"MM/dd/yy HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:now];
NSLog(@"%@",dateString);
Vignesh
  • 10,205
  • 2
  • 35
  • 73
1

Between the following two threads, I think you'll find everything you need (and thensome). The first is an extensive example of a problem similar to yours (just remember to look at the code in the answers and not the question), while the second has all the time zone abbreviations that you'll ever need. Gotta love the helpful people on The Stack.

The links again, just in case

objective-c: Conversion between TimeZones

GMT timezone conversion in objective c

Community
  • 1
  • 1
Kevin
  • 302
  • 2
  • 11
0
NSDate *localDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = @"MM/dd/yy";

NSString *dateString = [dateFormatter stringFromDate: localDate];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc]init];
timeFormatter.dateFormat = @"HH:mm:ss";


NSString *dateString = [timeFormatter stringFromDate: localDate];
Monikanta
  • 307
  • 2
  • 8