1

In my iPhone application, I somehow need the time value and get it like:

//Get the full time here
NSDate *now = [NSDate date];
//Seperate as time and date
NSArray *arr = [[[NSString alloc] initWithFormat:@"%@",now] componentsSeparatedByString:@" "];
//Get the time value
NSString *timeOnly = [arr objectAtIndex:1];
//Seperate it like hour, minute and second values
NSArray *timeOnlyArray = [timeOnly componentsSeparatedByString:@":"];
NSInteger hour = [[timeOnlyArray objectAtIndex:0] intValue];
NSInteger minute = [[timeOnlyArray objectAtIndex:1] intValue];

It all works fine except one thing: Even though my computer's and my simulator's times are set to my local time (GMT +2) the values I get while debugging on simulator are GMT time (2 hours earlier than my local).

That's fine, I can simply add 2 to my hour value but I don't know if it will be same on device. If I publish it this way, will it work same on device? Or should I add 2 hours because the application will look to GMT time in the real device too?

kubilay
  • 5,047
  • 7
  • 48
  • 66

1 Answers1

1

You might want to look into the NSDateFormatter class (as written in the AppleDocs: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html) When you've done that read up here: Convert UTC NSDate to local Timezone Objective-C

Just adding the 2 to the hours-value isn't good practice. How will you manage with daylight saving time? Or when someone in another timezone will use your app?

Using the NSDateFormatter will probably be the best solution for you.

Community
  • 1
  • 1
Totumus Maximus
  • 7,543
  • 6
  • 45
  • 69