Jay*_*yas 6 timezone datetime nstimezone ios swift
let timeZone = NSTimeZone.system.description
let localTimeZone = TimeZone.ReferenceType.local.description
let currentTimeZone = TimeZone.current.description
let defaultTimeZone = TimeZone.ReferenceType.default.description
let autoUpdateTimezon = TimeZone.autoupdatingCurrent.description
print ("System Timezone \(timeZone)")
print ("Local Timezone \(localTimeZone)")
print ("Current Timezone \(currentTimeZone)")
print ("Default Timezone \(defaultTimeZone)")
print ("Auto updating Timezone \(autoUpdateTimezon)")
Run Code Online (Sandbox Code Playgroud)
输出
亚洲系统时区/加尔各答(当前)
本地时区亚洲/加尔各答(自动更新当前)
当前时区亚洲/加尔各答(当前)
默认时区亚洲/加尔各答(当前)
自动更新时区亚洲/加尔各答 (autoupdatingCurrent)
所以,我得到所有的输出都是一样的,所以这些时区之间有什么区别,在这种情况下我们应该使用哪个时区。
问题
我使用以下代码进行日期转换
static func stringToString(strDate:String, fromFormat:String, toFormat:String)->String{
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.init(abbreviation: "UTC") ?? TimeZone(identifier: "UTC") ?? TimeZone.ReferenceType.default
dateFormatter.dateFormat = fromFormat
let currentDate = dateFormatter.date(from: strDate) ?? Date()
dateFormatter.dateFormat = toFormat
dateFormatter.timeZone = TimeZone.ReferenceType.default
let currentDates = dateFormatter.string(from: currentDate)
return currentDates
}
Run Code Online (Sandbox Code Playgroud)
场景:如果用户自动设置时区
off the 24 hours,我的应用程序在卡塔尔崩溃,但在印度没有崩溃 (TimeZone.ReferenceType.local)我已经给出了下一个构建
TimeZone.ReferenceType.default并且问题已解决所以,我无法理解是什么问题。
崩溃报告
我崩溃的旧代码
当地的 -> 跟踪当前系统时区的对象。当您想要一个始终反映当前系统时区的对象时,请使用此属性。从 iOS 11 开始,local 类属性反映当前系统时区,而以前它反映默认时区。
\n\n系统-> 系统当前使用的时区。如果您访问系统类属性,其值将由应用程序缓存,并且如果用户随后更改系统时区,则该值不会更新。为了使系统属性反映新的时区,您必须首先调用该resetSystemTimeZone()方法来清除缓存的值。
默认-> 当前应用程序的默认时区。如果没有设置默认时区,则使用当前系统时区。如果无法确定当前系统时区,则使用 GMT 时区。应用程序使用默认时区进行日期和时间操作。您可以将其设置为使应用程序像在不同时区一样运行。
\n\n当前-> 系统当前使用的时区。
\n\nautoupdatingCurrent -> 系统当前使用的时区,自动更新为用户\xe2\x80\x99s当前首选项。
\n\n来源 -> https://developer.apple.com/documentation/foundation/nstimezone
\n| 归档时间: |
|
| 查看次数: |
1475 次 |
| 最近记录: |