Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDate set timezone in swift

Tags:

swift

nsdate

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
like image 698
Mugunthan Balakrishnan Avatar asked Aug 17 '15 09:08

Mugunthan Balakrishnan


People also ask

How do I change the current timeZone in Swift?

Standard Approach, using a DateFormatter() đŸ—“current timeZone. This will be set to . current by default, but it is important to note in case you need to set anything other that the current timeZone that you exist in.

How to create date with timeZone in swift?

In Swift, a Date() value encapsulates a single point in time, independent of any particular time zone or calendarical system. To accomplish this, a Date() value is stored as a 64-bit floating point number counting the number of seconds as a relative offset from the reference date of January 1, 2001 at 00:00:00 UTC.

How do you convert UTC to local time in Swift?

I want to convert server UTC time to local time and vice-versa. Here is my code.. var isTimeFromServer = true var time:String! var period:String! let timeString = "6:59 AM" //Current UTC time if isTimeFromServer { let index = timeString.


2 Answers

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")
}
like image 108
Michael Montalbano Avatar answered Sep 17 '22 14:09

Michael Montalbano


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)

like image 31
mfaani Avatar answered Sep 21 '22 14:09

mfaani