swift - 如何在 ios swift 中将 UNIX 纪元时间转换为日期和时间

标签 swift datetime unix-timestamp

我正在尝试使用以下代码将 UNIX epoc 时间转换为日期时间格式

var epocTime = NSTimeInterval(1429162809359)

let myDate = NSDate(timeIntervalSince1970: epocTime)
println("Converted Time \(myDate)")

实际结果是(星期四,2015 年 4 月 16 日 05:40:09 GMT),但我得到的结果类似于 (47258-05-14 05:15:59 +0000) 谁能告诉我如何实现这一点。

最佳答案

更新:Xcode 8.2.1 • Swift 3.0.2 或更高版本

您需要将它从毫秒除以 1000 转换为:

let epochTime = TimeInterval(1429162809359) / 1000
let date = Date(timeIntervalSince1970: epochTime)   // "Apr 16, 2015, 2:40 AM"

print("Converted Time \(date)")         // "Converted Time 2015-04-16 05:40:09 +0000\n"

关于swift - 如何在 ios swift 中将 UNIX 纪元时间转换为日期和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29670585/

相关文章:

iOS 9 UICollectionView 水平重新加载数据不起作用

java - java8中如何在LocalDateTime、ZonedDateTime等类中选择Date/Time类?

Python 和 Pandas : How to round Up/Down unix timestamp (utc) on different resolutions: 1min-5min-15min-30min-1H-1D?

c++ - gmtime算法的最小实现?

python - 使用 pandas 将 Unix 13 位数字转换为日期时间/时间戳格式

ios - Master Detail View 我想回到主视图而不是详细 View

ios - 当我在 View Controller 中移动时保存文本字段的值

ios - Swift:单元测试页面跳转

javascript - 获取与系统时钟分开的实时时间

python - 计算新 DataFrame 列中给定日期以来的月数