what time is it in that city?

My iOS app needs to know what hour of the day it is in a particular city.

NSDate *date = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
[calendar setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Canberra"]];

NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
NSInteger hour = [dateComponents hour];

if (hour > 2 && hour < 12) { // app is only available between midday and 2am

// tell the user to come back later


To see a list of available timezone names, launch Terminal on OSX and type:

ls /usr/share/zoneinfo/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s