ios Swift 3 时区问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39910001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift 3 timezone issue
提问by AmrataB
It seems setDefaultTimeZone
method is no longer available in NSTimeZone
. Does someone know a substitute for this?
似乎setDefaultTimeZone
方法在NSTimeZone
. 有人知道这个的替代品吗?
In my AppDelegate.swift
, I have:
在我的AppDelegate.swift
,我有:
NSTimeZone.default = TimeZone(abbreviation: "BST")!
and it works as intended I guess because in all the other files, I get NSTimeZone
set to this value
我猜它按预期工作,因为在所有其他文件中,我都NSTimeZone
设置为这个值
Now, In my Utils, I have this method:
现在,在我的实用程序中,我有这个方法:
static func getDate(_ dateStr: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
// dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
let date = dateFormatter.date(from: dateStr)!
return date
}
So, lets say, I give it input 2016-10-07
, it gives me back 2016-10-06 23:00
. Why? It gets fixed if you uncomment the line in the above code. I don't want to use this line everywhere.
所以,让我们说,我给它输入2016-10-07
,它给我回2016-10-06 23:00
。为什么?如果您取消注释上述代码中的行,它就会得到修复。我不想到处都使用这条线。
For example, in some other part of my project, I have used CVCalendar
. It provides a function for getting convertedDate like so
例如,在我项目的其他部分,我使用了CVCalendar
. 它提供了一个像这样获取convertedDate的函数
func didSelectDayView(_ dayView: DayView, animationDidFinish: Bool) {
selectedDay = dayView
selectedDate = dayView.date.convertedDate()!
}
The same thing as before is happening here too...that is I click on 2016-10-08
and it selectedDate
here becomes 2016-10-07 23:00
.
这里也发生了和以前一样的事情......那是我点击它2016-10-08
,它selectedDate
在这里变成2016-10-07 23:00
.
And the NSTimeZone.Default
prints Europe/London
everywhere.
而且到处都是NSTimeZone.Default
印记Europe/London
。
Does anyone have any idea why is this happening?
有谁知道为什么会这样?
采纳答案by Jitendra Modi
let locale = NSTimeZone.init(abbreviation: "BST")
NSTimeZone.default = locale as! TimeZone
Try this
尝试这个
回答by Nirav D
Try like this.
像这样尝试。
TimeZone.ReferenceType.default = TimeZone(abbreviation: "BST")!
Edit:I have used this TimeZone
with DateFormatter
and get the correct BST
time with date.
编辑:我已经使用这个TimeZone
与DateFormatter
和得到正确的BST
时间和日期。
TimeZone.ReferenceType.default = TimeZone(abbreviation: "BST")!
let formatter = DateFormatter()
formatter.timeZone = TimeZone.ReferenceType.default
formatter.dateFormat = "yyyy-MM-dd HH:mm"
let strDate = formatter.string(from: Date())
print(strDate)
If you want to set defaultTimeZone
for NSTimeZone
object then in Swift 3 you can set like this.
如果你想设置defaultTimeZone
的NSTimeZone
对象,然后在斯威夫特3,你可以这样设置。
NSTimeZone.default = TimeZone(abbreviation: "BST")!
回答by Wolverine
If you are using calendar then,
如果您正在使用日历,那么
// * create calendar object *
// * 创建日历对象 *
var calendar = NSCalendar.current
// * define calendar components to use as well Timezone to UTC *
// * 定义日历组件也使用时区到 UTC *
let unitFlags = Set<Calendar.Component>([.hour, .year, .minute])
calendar.timeZone = TimeZone(identifier: "UTC")!
/// Returns a time zone initialized with a given identifier. /// /// An example identifier is "America/Los_Angeles". /// /// If `identifier` is an unknown identifier, then returns `nil`. public init?(identifier: String)
The geopolitical region identifier that identifies the time zone.
public var identifier: String { get }
/// Returns a time zone initialized with a given identifier. /// /// An example identifier is "America/Los_Angeles". /// /// If `identifier` is an unknown identifier, then returns `nil`. public init?(identifier: String)
标识时区的地缘区域标识符。
公共变量标识符:字符串{获取}
Note: If you want to set the date formatter timezone, you can follow the above approach like this :
注意:如果要设置日期格式化程序时区,可以按照上述方法进行操作:
dateFormatter.timeZone = TimeZone(identifier: "UTC")!