如何在ios swift中将UNIX epoc时间转换为日期和时间 [英] How to Convert UNIX epoc time to Date and time in ios swift
本文介绍了如何在ios swift中将UNIX epoc时间转换为日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下代码将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日05:40:09 GMT),但我得到的东西像(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
update: Xcode 8.2.1 • Swift 3.0.2
您需要将它从毫秒除以1000来转换:
You need to convert it from milliseconds dividing it by 1000:
let epocTime = TimeInterval(1429162809359) / 1000
let myDate = Date(timeIntervalSince1970: epocTime) // "Apr 16, 2015, 2:40 AM"
print("Converted Time \(myDate)") // "Converted Time 2015-04-16 05:40:09 +0000\n"
这篇关于如何在ios swift中将UNIX epoc时间转换为日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文