[英]NSDate timeIntervalSince1970 not working in Swift?
I am doing this in swift: 我正在迅速这样做:
let date = NSDate(timeIntervalSince1970: 1432233446145.0)
println("date is \(date)")
The log gives me this: 日志给了我这个:
date is 47355-09-02 23:55:45 +0000
Then I try to get an interval back out just for testing: 然后,我尝试返回间隔以进行测试:
let tI = expirationDate.timeIntervalSinceDate(date)
println("tI = \(tI)")
I get: 我得到:
tI = 0.0 tI = 0.0
What am I doing wrong? 我究竟做错了什么? I can seem to make the timeIntervalSince1970 call work properly.
我似乎可以使timeIntervalSince1970调用正常工作。 Is there any known issued with that in Swift, or am I missing something here?
在Swift中是否有与此相关的已知问题,或者我在这里缺少什么?
1432233446145
most probably is a time interval given in milliseconds : 1432233446145
最有可能是一个以毫秒为单位的时间间隔:
let date = NSDate(timeIntervalSince1970: 1432233446145.0/1000.0)
print("date is \(date)")
// date is 2015-05-21 18:37:26 +0000
Swift 3 and later: Swift 3及更高版本:
let date = Date(timeIntervalSince1970: 1432233446145.0/1000.0)
I think your timestamp is incorrect. 我认为您的时间戳不正确。 This results in your date being september 2nd, 47355.
结果您的日期是47355年9月2日。
If I use the following I get the date for (right about) now: 如果我使用以下命令,我现在就知道(正确的日期)日期:
let date = NSDate(timeIntervalSince1970: 1431024488)
println("date is \(date)")
// "date is 2015-05-07 18:48:08 +0000"
The printed date is not a localized timestamp, so you'll have to do some localization of your own I suppose. 打印日期不是本地化的时间戳,所以我想您必须对自己的日期进行一些本地化。 An example:
一个例子:
let formatter = NSDateFormatter()
formatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
println("formatted date is \(formatter.stringFromDate(date))")
// "formatted date is 07-05-2015 20:48:08"
And for completeness I also checked with an expiration date that's 1100 seconds larger than the initial date
: 为了完整起见,我还检查了比初始
date
大1100秒的失效date
:
let expirationDate = NSDate(timeIntervalSince1970: 1431025588)
let diff = expirationDate.timeIntervalSinceDate(date)
println("expires in: \(diff)")
// "expires in: 1100.0"
So, the timeIntervalSince1970 seems to work fine, I think your interval was just not what you wanted it to be. 因此,timeIntervalSince1970似乎运行良好,我认为您的间隔不是您想要的间隔。
From your code and the log content follows: 从您的代码和日志内容如下:
date.isEqual(expirationDate)
--> Your stuff has just expired :-). ->您的资料刚刚过期:-)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.