如何在iOS Swift中将UNIX纪元时间转换为日期和时间 [英] How to Convert UNIX epoch time to Date and time in ios swift

查看:76
本文介绍了如何在iOS Swift中将UNIX纪元时间转换为日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to convert the UNIX epoc time to datetime format using the below code

var epocTime = NSTimeInterval(1429162809359)

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

实际结果是(格林尼治标准时间2015年4月16日,星期四),但得到的是类似(47258-05-14 05:15:59 +0000)的东西,有人可以给我打电话告诉我如何实现这一目标./p>

the actual result is (Thu, 16 Apr 2015 05:40:09 GMT) but am getting something like (47258-05-14 05:15:59 +0000) Can anyone please tel me how to achieve this.

推荐答案

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

您需要将其转换为毫秒数除以1000:

You need to convert it from milliseconds dividing it by 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"

这篇关于如何在iOS Swift中将UNIX纪元时间转换为日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆