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

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

问题描述

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

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 日星期四 05:40:09),但得到的结果类似于 (47258-05-14 05:15:59 +0000) 任何人都可以告诉我如何实现这一点.

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
"

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

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