Mugunthan Balakrishnan
Mugunthan Balakrishnan

Reputation: 865

NSDate set timezone in swift

how can i return a NSDate in a predefined time zone from a string

let responseString = "2015-8-17 GMT+05:30"
var dFormatter = NSDateFormatter()
dFormatter.dateFormat = "yyyy-M-dd ZZZZ"
var serverTime = dFormatter.dateFromString(responseString)
println("NSDate : \(serverTime!)")

the above code returns the time as

2015-08-16 18:30:00 +0000

Upvotes: 14

Views: 63103

Answers (5)

mfaani
mfaani

Reputation: 36287

The number 1 means 1 regardless of language. Yet in English it's spelled as one, in Spanish it's una, in Arabic it wahid, etc.

Similarly 123982373 seconds pass 1970 is going to reflect differently in different timezones or calendar formats, but's all still 123982373 seconds passed 1970


The difference between 3 seconds and 7 seconds is 4 seconds. That doesn't require a calendar. Neither you need a calendar/timezone to know the difference in time between these two Epoch times 1585420200 and 1584729000

Dates are just a timeInterval from January 1, 1970 (midnight UTC/GMT). Dates also happen to have a string representation.

Repeating Nikolia's answer, Swift's default string interpolation (2015-08-16 18:30:00 +0000) uses a DateFormatter that uses UTC (that's the "+0000" in the output).

Calendars with the use of timezones give us a contextual representation that is just easier to understand than trying to calculate the difference between two gigantic numbers.

Meaning a single date (think of a single timeInterval since 1970) will have a different string interpretations per calendar. On top of that a calendar will itself vary based on time zones

I highly recommend that you go and play around with this Epoch converter site and see how selecting a different timezone will cause the string representations for the same moment/date/timeInterval to change


I also recommend to see this answer. Mainly this part:

Timezone is just an amendment to the timestamp string, it's not considered by the date formatter.

To consider the time zone you have to set the timeZone of the formatter

dateFormatter.timeZone = TimeZone(secondsFromGMT: -14400)

Upvotes: 1

Michael Montalbano
Michael Montalbano

Reputation: 480

If you always have the same time zone for the input string, you can create two date formatters to output the local time zone (or a specified one):

let timeFormatterGet = DateFormatter()
timeFormatterGet.dateFormat = "h:mm a"
timeFormatterGet.timeZone = TimeZone(abbreviation: "PST")

let timeFormatterPrint = DateFormatter()
timeFormatterPrint.dateFormat = "h:mm a"
// timeFormatterPrint.timeZone = TimeZone(abbreviation: "EST") // if you want to specify timezone for output, otherwise leave this line blank and it will default to devices timezone

if let date = timeFormatterGet.date(from: "3:30 PM") {
    print(timeFormatterPrint.string(from: date)). // "6:30 PM" if device in EST
} else {
   print("There was an error decoding the string")
}

Upvotes: 1

Saumil Shah
Saumil Shah

Reputation: 2349

The date format has to be assigned to the dateFormat property of the date formatter instead.

let date = NSDate.date()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let str = dateFormatter.stringFromDate(date)
println(str)

This prints the date using the default time zone on the device. Only if you want the output according to a different time zone then you would add for example

Swift 3.*

dateFormatter.timeZone = NSTimeZone(name: "UTC")

Swift 4.*

dateFormatter.timeZone = TimeZone(abbreviation: "UTC")

also refer link http://www.brianjcoleman.com/tutorial-nsdate-in-swift/

Upvotes: 44

Amr
Amr

Reputation: 2280

Swift 4.0

dateFormatter.timeZone = TimeZone(abbreviation: "UTC")

Upvotes: 12

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81868

how can i return a NSDate in a predefined time zone?

You can't.

An instance of NSDate does not carry any information about timezone or calendar. It just simply identifies one point in universal time.

You can interpret this NSDate object in whatever calendar you want. Swift's string interpolation (the last line of your example code) uses an NSDateFormatter that uses UTC (that's the "+0000" in the output).

If you want the NSDate's value as a string in the current user's calendar you have to explicitly set up a date formatter for that.

Upvotes: 19

Related Questions