存储时间戳和当前日期之间的差异 [英] Difference between timestamp stored and current date
问题描述
该应用程序将Firebase的时间作为 我想要这个数字来区分用户是否按下了同一个按钮的时间小于48小时以前。 有没有办法测量48小时的时间段还是应该用不同的方法? p> I have linked up an app that I have made to an Firebase database. The app sends Firebase the time as a I want this number to distinguish wether the user has pressed that same button more less than 48 hours ago. Is there a way to measure the time period of 48 hours or should I use a different method? I am using swift 2 and Xcode 7! Swift 3. If you are using Swift 2, use NSDate
这篇关于存储时间戳和当前日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! timeIntervalSinceReferenceDate
当一个按钮被按下时。这个值的一个例子是 - 498898978852.928 。
<如果您使用的是Swift 2,请使用NSDate
//第一个按钮按下,这是一个double,但是假设它是一个timeInterval
let firstPress = 498898978.928
let date = Date()
let secondPress = date.timeIntervalSinceReferenceDate //第二个按钮按下
let diffInSeconds = secondPress - firstPress //总时间差秒
let hours = diffInSeconds / 60/60 // hours = diff in seconds / 60 sec per min / 60 min per hour
if hours<
print(小于48小时的差额)
} else {
print(大于/等于48小时差额)
}
timeIntervalSinceReferenceDate
when a button is pressed. An example of the value is - 498898978852.928.//the first button press, this is a double but assume it's a timeInterval
let firstPress = 498898978.928
let date = Date()
let secondPress = date.timeIntervalSinceReferenceDate //the second button press
let diffInSeconds = secondPress - firstPress //total time difference in seconds
let hours = diffInSeconds/60/60 //hours=diff in seconds / 60 sec per min / 60 min per hour
if hours < 48 {
print("less then 48 hours difference")
} else {
print("greater/equal to 48 hours difference")
}