AAV*_*AAV 11 timezone date objective-c ios swift
输入字符串:2012年6月14日 - 01:00:00 UTC
输出本地字符串:美国东部时间2012年6月13日 - 21:00:00
我喜欢从中获得偏移
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSLog(@"Time Zone: %@", destinationTimeZone.abbreviation);
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?
das*_*ght 23
这应该做你需要的:
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"LLL d, yyyy - HH:mm:ss zzz";
NSDate *utc = [fmt dateFromString:@"June 14, 2012 - 01:00:00 UTC"];
fmt.timeZone = [NSTimeZone systemTimeZone];
NSString *local = [fmt stringFromDate:utc];
NSLog(@"%@", local);
Run Code Online (Sandbox Code Playgroud)
请注意,您的示例不正确:当它在6月14日凌晨1点在UTC时,它仍然是美国东部时间6月13日,标准时间下午8点或夏令时晚9点.在我的系统上,该程序打印
Jun 13, 2012 - 21:00:00 EDT
Run Code Online (Sandbox Code Playgroud)
斯威夫特 3
var dateformat = DateFormatter()
dateformat.dateFormat = "LLL d, yyyy - HH:mm:ss zzz"
var utc: Date? = dateformat.date(fromString: "June 14, 2012 - 01:00:00 UTC")
dateformat.timeZone = TimeZone.current
var local: String = dateformat.string(from: utc)
print(local)
Run Code Online (Sandbox Code Playgroud)
Swift 4:日期扩展 UTC 或 GMT ?当地的
//UTC or GMT ? Local
extension Date {
// Convert local time to UTC (or GMT)
func toGlobalTime() -> Date {
let timezone = TimeZone.current
let seconds = -TimeInterval(timezone.secondsFromGMT(for: self))
return Date(timeInterval: seconds, since: self)
}
// Convert UTC (or GMT) to local time
func toLocalTime() -> Date {
let timezone = TimeZone.current
let seconds = TimeInterval(timezone.secondsFromGMT(for: self))
return Date(timeInterval: seconds, since: self)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17928 次 |
| 最近记录: |