存储的时间戳和当前日期之间的差异
Difference between timestamp stored and current date
我已经将我制作的应用程序链接到 Firebase 数据库。
应用程序在按下按钮时将时间作为 timeIntervalSinceReferenceDate
发送给 Firebase。该值的示例是 - 498898978852.928.
我希望这个数字能够区分用户是否在 48 小时前按下了同一个按钮。
有没有办法测量 48 小时的时间段,或者我应该使用其他方法?
我正在使用 swift 2 和 Xcode 7!
您正在使用 unix timeStamp*1000,当转换为 Int 时,它被用作时间戳,但由于您将使用它进行时间操作。用它来存储你的时间戳:-
let timeStamp = NSDate.timeIntervalSinceReferenceDate // Like this only
在您的 class 范围之外扩展 NSDate
extension NSDate {
func daysFromTheDate(date: NSDate) -> Int {
return NSCalendar.currentCalendar().components(.Day, fromDate: date, toDate: self, options: []).day
}
}
class yourViewController : UIViewController,.. {
...
}
声明:-
let date1 = NSDate() // Declare the variable globally
然后在你的 .observe 事件 firebase 函数中使用这个:-
var retrieved_timeStamp = ... // timeStamp that you retrieve from firebase
var date = NSDate(timeIntervalSinceReferenceDate: timeInterval(retrieved_timeStamp))
print(date1.daysFromTheDate(from: date as NSDate))
检查条件
if date1.daysFromTheDate(from: date as NSDate) >= 2{
//48 or more hours have passed now, Do the Job. Bingo!..
}
有关进一步的日期操作,请查找:- Difference between two NSDates
Swift 3.如果你正在使用Swift 2,使用NSDate
//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")
}
我已经将我制作的应用程序链接到 Firebase 数据库。
应用程序在按下按钮时将时间作为 timeIntervalSinceReferenceDate
发送给 Firebase。该值的示例是 - 498898978852.928.
我希望这个数字能够区分用户是否在 48 小时前按下了同一个按钮。
有没有办法测量 48 小时的时间段,或者我应该使用其他方法?
我正在使用 swift 2 和 Xcode 7!
您正在使用 unix timeStamp*1000,当转换为 Int 时,它被用作时间戳,但由于您将使用它进行时间操作。用它来存储你的时间戳:-
let timeStamp = NSDate.timeIntervalSinceReferenceDate // Like this only
在您的 class 范围之外扩展 NSDate
extension NSDate {
func daysFromTheDate(date: NSDate) -> Int {
return NSCalendar.currentCalendar().components(.Day, fromDate: date, toDate: self, options: []).day
}
}
class yourViewController : UIViewController,.. {
...
}
声明:-
let date1 = NSDate() // Declare the variable globally
然后在你的 .observe 事件 firebase 函数中使用这个:-
var retrieved_timeStamp = ... // timeStamp that you retrieve from firebase
var date = NSDate(timeIntervalSinceReferenceDate: timeInterval(retrieved_timeStamp))
print(date1.daysFromTheDate(from: date as NSDate))
检查条件
if date1.daysFromTheDate(from: date as NSDate) >= 2{
//48 or more hours have passed now, Do the Job. Bingo!..
}
有关进一步的日期操作,请查找:- Difference between two NSDates
Swift 3.如果你正在使用Swift 2,使用NSDate
//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")
}