我是iOS开发的新手。 我正在尝试构建一个主要可以做两件事的应用程序:
一个。 获取用户的系统时间(比如,他的手机在伦敦,所以他的时间)
湾在指定地点(比如旧金山)获得时间
然后,我想计算一下这个位置之间的时差(例如伦敦,英国比加利福尼亚州旧金山早8小时)
有人可以帮我提出如何获得(a)和(b)的建议吗?
答案 0 :(得分:2)
对于(a)和(b):
let offset = TimeZone.current.secondsFromGMT();
print(offset)//Your current timezone offset in seconds
let loc = CLLocation.init(latitude: 48.8566, longitude: 2.3522);//Paris's lon/lat
let coder = CLGeocoder();
coder.reverseGeocodeLocation(loc) { (placemarks, error) in
let place = placemarks?.last;
let newOffset = place?.timeZone?.secondsFromGMT();
print(newOffset);//Paris's timezone offset in seconds
}
答案 1 :(得分:0)
如果您正在寻找当地时间,可以使用:
NSTimeZone *timeZoneLocal = [NSTimeZone localTimeZone];
NSString *strTimeZoneName = [timeZoneLocal name];
or
NSTimeZone *timeZoneLocal = [NSTimeZone defaultTimeZone];
NSString *strTimeZoneName = [timeZoneLocal name];
您可能只需要两个文本字段,并让用户比较时差。