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(timeResult)= 1415639000.67457
println(NSDate())
= 2014-11-10 17:03:20 +0000 was just to test to see what NSDate
was providing.
println(NSDate() = 2014-11-10 17:03:20 +0000只是测试看看NSDate提供了什么。
I want the first to look like the last. The value for dt = 1415637900.
我希望第一个看起来像最后一个。dt的值等于1415637900。
Also, how can I adjust to time zone? Running on iOS.
另外,我如何适应时区?在iOS上运行。
8 个解决方案
#1
77
You can get a date with that value by using the NSDate(withTimeIntervalSince1970:)
initializer:
通过使用NSDate(带有timeintervalsince1970:)初始化器:
let date = NSDate(timeIntervalSince1970: 1415637900)
#2
26
It's simple to convert the Unix timestamp into the desired format. Lets suppose _ts is the Unix timestamp in long
将Unix时间戳转换为所需的格式很简单。假设_ts是Unix时间戳
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)")
#3
24
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)
self.defaults.setObject(localDate, forKey: "localdate")
}
Swift 3.0 Version
斯威夫特3.0版本
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)
}
#4
10
For managing dates in Swift 3 I ended up with this helper function:
对于Swift 3中的日期管理,我最终实现了这个助手功能:
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)
有关格式化的更多细节,请参考DateFormatter文档(请注意,有些示例已经过时)
I found this article to be very helpful as well
我发现这篇文章也很有帮助
#5
4
func timeStringFromUnixTime(unixTime: Double) -> String {
let date = NSDate(timeIntervalSince1970: unixTime)
// Returns date formatted as 12 hour time.
dateFormatter.dateFormat = "hh:mm a"
return dateFormatter.stringFromDate(date)
}
func dayStringFromTime(unixTime: Double) -> String {
let date = NSDate(timeIntervalSince1970: unixTime)
dateFormatter.locale = NSLocale(localeIdentifier: NSLocale.currentLocale().localeIdentifier)
dateFormatter.dateFormat = "EEEE"
return dateFormatter.stringFromDate(date)
}
#6
4
Here is a working Swift 3 solution from one of my apps.
这是我的一个应用的Swift 3解决方案。
/**
*
* 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)"
}
#7
2
Swift:
迅速:
extension Double {
func getDateStringFromUnixTime(dateStyle: DateFormatter.Style, timeStyle: DateFormatter.Style) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = dateStyle
dateFormatter.timeStyle = timeStyle
return dateFormatter.string(from: Date(timeIntervalSince1970: self))
}
}
#8
1
Anyway @Nate Cook's answer is accepted but I would like to improve it with better date format.
无论如何,@Nate Cook的答案是可以接受的,但是我想用更好的日期格式来改进它。
with Swift 2.2, I can get desired formatted date
使用Swift 2.2,我可以得到想要的格式化日期。
//TimeStamp
let timeInterval = 1415639000.67457
print("time interval is \(timeInterval)")
//Convert to Date
let date = NSDate(timeIntervalSince1970: timeInterval)
//Date formatting
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd, MMMM yyyy HH:mm:a"
dateFormatter.timeZone = NSTimeZone(name: "UTC")
let dateString = dateFormatter.stringFromDate(date)
print("formatted date is = \(dateString)")
the result is
结果是
time interval is 1415639000.67457
时间间隔是1415639000.67457
formatted date is = 10, November 2014 17:03:PM
格式日期为:2014年11月10日17:03:PM
#1
77
You can get a date with that value by using the NSDate(withTimeIntervalSince1970:)
initializer:
通过使用NSDate(带有timeintervalsince1970:)初始化器:
let date = NSDate(timeIntervalSince1970: 1415637900)
#2
26
It's simple to convert the Unix timestamp into the desired format. Lets suppose _ts is the Unix timestamp in long
将Unix时间戳转换为所需的格式很简单。假设_ts是Unix时间戳
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)")
#3
24
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)
self.defaults.setObject(localDate, forKey: "localdate")
}
Swift 3.0 Version
斯威夫特3.0版本
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)
}
#4
10
For managing dates in Swift 3 I ended up with this helper function:
对于Swift 3中的日期管理,我最终实现了这个助手功能:
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)
有关格式化的更多细节,请参考DateFormatter文档(请注意,有些示例已经过时)
I found this article to be very helpful as well
我发现这篇文章也很有帮助
#5
4
func timeStringFromUnixTime(unixTime: Double) -> String {
let date = NSDate(timeIntervalSince1970: unixTime)
// Returns date formatted as 12 hour time.
dateFormatter.dateFormat = "hh:mm a"
return dateFormatter.stringFromDate(date)
}
func dayStringFromTime(unixTime: Double) -> String {
let date = NSDate(timeIntervalSince1970: unixTime)
dateFormatter.locale = NSLocale(localeIdentifier: NSLocale.currentLocale().localeIdentifier)
dateFormatter.dateFormat = "EEEE"
return dateFormatter.stringFromDate(date)
}
#6
4
Here is a working Swift 3 solution from one of my apps.
这是我的一个应用的Swift 3解决方案。
/**
*
* 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)"
}
#7
2
Swift:
迅速:
extension Double {
func getDateStringFromUnixTime(dateStyle: DateFormatter.Style, timeStyle: DateFormatter.Style) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = dateStyle
dateFormatter.timeStyle = timeStyle
return dateFormatter.string(from: Date(timeIntervalSince1970: self))
}
}
#8
1
Anyway @Nate Cook's answer is accepted but I would like to improve it with better date format.
无论如何,@Nate Cook的答案是可以接受的,但是我想用更好的日期格式来改进它。
with Swift 2.2, I can get desired formatted date
使用Swift 2.2,我可以得到想要的格式化日期。
//TimeStamp
let timeInterval = 1415639000.67457
print("time interval is \(timeInterval)")
//Convert to Date
let date = NSDate(timeIntervalSince1970: timeInterval)
//Date formatting
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd, MMMM yyyy HH:mm:a"
dateFormatter.timeZone = NSTimeZone(name: "UTC")
let dateString = dateFormatter.stringFromDate(date)
print("formatted date is = \(dateString)")
the result is
结果是
time interval is 1415639000.67457
时间间隔是1415639000.67457
formatted date is = 10, November 2014 17:03:PM
格式日期为:2014年11月10日17:03:PM