16

It seems setDefaultTimeZone method is no longer available in NSTimeZone. Does someone know a substitute for this?

In my AppDelegate.swift, I have:

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

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.

For example, in some other part of my project, I have used CVCalendar. It provides a function for getting convertedDate like so

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.

And the NSTimeZone.Default prints Europe/London everywhere.

Does anyone have any idea why is this happening?

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
AmrataB
  • 1,066
  • 2
  • 10
  • 19

3 Answers3

14

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.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.

NSTimeZone.default = TimeZone(abbreviation: "BST")!
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • @AmrataB By setting this `NSTimeZone.default = TimeZone(abbreviation: "BST")!` you are only set default time zone not using it and When you have use dateFormatter set specific time zone with it other wise it may be used your system or local one. – Nirav D Oct 07 '16 at 08:46
  • @AmrataB Are you getting what I am trying to say? – Nirav D Oct 07 '16 at 12:02
  • You do not have to specify TimeZone separately for DateFormatter. It picks the one you set on NSTimeZone default or TimeZone default. My issue was that before swift 3, it used to consider daylight savings that is if you print timezone, it used to give this Europe/London (GMT+1) offset 3600 (Daylight) and so everything worked for me. But now, it has started returning Europe/London or simply one hour minus UTC. This broke everything. So, I set my default to UTC now so that it will not break again after one month. – AmrataB Oct 08 '16 at 04:12
  • @AmrataB I am not sure what you are saying is correct, because i have first tried to set default time zone in playground with BST and then try to get the current time, it is not the BST it was my current timezone, after setting the timezone with dateFormatter i will able to get current BST time, May be you miss understanding check properly once again. – Nirav D Oct 08 '16 at 04:58
  • @AmrataB I have post same code on `IBM Swift Sandbox`, please check this link once and try to run the code first and then uncomment the line that specify time zone for dateFormatter, You will Know the difference, Check this link https://swiftlang.ng.bluemix.net/#/repl/57f885c0ed27cb2723a1cb52 First you will get the time of that IBM sand box server's current time and if you uncomment you will get the current BST time. – Nirav D Oct 08 '16 at 05:41
  • I don't understand why this works: TimeZone.ReferenceType.default = TimeZone(abbreviation: "UTC")! dateFormatter.timeZone = TimeZone.ReferenceType.default and this fails: dateFormatter.timeZone = TimeZone(abbreviation: "UTC") to produce the datetime in Z the timezone. – James Y Aug 03 '17 at 22:17
  • Saved my goddamn day! – Codetard Oct 09 '18 at 04:48
10
let locale = NSTimeZone.init(abbreviation: "BST")
NSTimeZone.default = locale as! TimeZone

Try this

Jitendra Modi
  • 2,344
  • 12
  • 34
2

If you are using calendar then,

// * create calendar object *

var calendar = NSCalendar.current

// * define calendar components to use as well Timezone to 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 }

Note : If you want to set the date formatter timezone, you can follow the above approach like this :

dateFormatter.timeZone = TimeZone(identifier: "UTC")!
Wolverine
  • 4,264
  • 1
  • 27
  • 49