Question or problem with Swift language programming:
My current code:
if let var timeResult = (jsonResult["dt"] as? Double) { timeResult = NSDate().timeIntervalSince1970 println(timeResult) println(NSDate()) }
The results:
println(timeResult) = 1415639000.67457
println(NSDate()) = 2014-11-10 17:03:20 +0000 was just to test to see what NSDate was providing.
I want the first to look like the last. The value for dt = 1415637900.
Also, how can I adjust to time zone? Running on iOS.
How to solve the problem:
Solution 1:
You can get a date with that value by using the NSDate(withTimeIntervalSince1970:)
initializer:
let date = NSDate(timeIntervalSince1970: 1415637900)
Solution 2:
To get the date to show as the current time zone I used the following.
if let timeResult = (jsonResult["dt"] as? Double) { let date = NSDate(timeIntervalSince1970: timeResult) let dateFormatter = NSDateFormatter() dateFormatter.timeStyle = NSDateFormatterStyle.MediumStyle //Set time style dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle //Set date style dateFormatter.timeZone = NSTimeZone() let localDate = dateFormatter.stringFromDate(date) }
Swift 3.0 Version
if let timeResult = (jsonResult["dt"] as? Double) { let date = Date(timeIntervalSince1970: timeResult) let dateFormatter = DateFormatter() dateFormatter.timeStyle = DateFormatter.Style.medium //Set time style dateFormatter.dateStyle = DateFormatter.Style.medium //Set date style dateFormatter.timeZone = self.timeZone let localDate = dateFormatter.string(from: date) }
Swift 5
if let timeResult = (jsonResult["dt"] as? Double) { let date = Date(timeIntervalSince1970: timeResult) let dateFormatter = DateFormatter() dateFormatter.timeStyle = DateFormatter.Style.medium //Set time style dateFormatter.dateStyle = DateFormatter.Style.medium //Set date style dateFormatter.timeZone = .current let localDate = dateFormatter.string(from: date) }
Solution 3:
It’s simple to convert the Unix timestamp into the desired format. Lets suppose _ts is the Unix timestamp in long
let date = NSDate(timeIntervalSince1970: _ts) let dayTimePeriodFormatter = NSDateFormatter() dayTimePeriodFormatter.dateFormat = "MMM dd YYYY hh:mm a" let dateString = dayTimePeriodFormatter.stringFromDate(date) print( " _ts value is \(_ts)") print( " _ts value is \(dateString)")
Solution 4:
For managing dates in Swift 3 I ended up with this helper function:
extension Double { func getDateStringFromUTC() -> String { let date = Date(timeIntervalSince1970: self) let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: "en_US") dateFormatter.dateStyle = .medium return dateFormatter.string(from: date) } }
This way it easy to use whenever you need it – in my case it was converting a string:
("1481721300" as! Double).getDateStringFromUTC() // "Dec 14, 2016"
Reference the DateFormatter docs for more details on formatting (Note that some of the examples are out of date)
I found this article to be very helpful as well
Solution 5:
Here is a working Swift 3 solution from one of my apps.
/** * * Convert unix time to human readable time. Return empty string if unixtime * argument is 0. Note that EMPTY_STRING = "" * * @param unixdate the time in unix format, e.g. 1482505225 * @param timezone the user's time zone, e.g. EST, PST * @return the date and time converted into human readable String format * **/ private func getDate(unixdate: Int, timezone: String) -> String { if unixdate == 0 {return EMPTY_STRING} let date = NSDate(timeIntervalSince1970: TimeInterval(unixdate)) let dayTimePeriodFormatter = DateFormatter() dayTimePeriodFormatter.dateFormat = "MMM dd YYYY hh:mm a" dayTimePeriodFormatter.timeZone = NSTimeZone(name: timezone) as TimeZone! let dateString = dayTimePeriodFormatter.string(from: date as Date) return "Updated: \(dateString)" }