Reputation: 725
I am determining the current time in swift with this code
let currentDate = Date().timeIntervalSince1970 * 1000
the result looks like
1493199752604.24
The obtained value is in milliseconds right? What is the .
inside the value?
I need to determine if the difference between two such date is equal or greater than 2 hours.
if (currentDate - oldDate >= 7200000){
// do something
}
is this code correct?
Upvotes: 3
Views: 6155
Reputation: 658
Yes it is working fine. Checkout this example for difference greater than 2 hours
let currentDate = Date().timeIntervalSince1970*1000
let calendar = NSCalendar.current
let yesteraysDate = calendar.date(byAdding: .day, value: -1, to: Date())
let oldDate = yesteraysDate!.timeIntervalSince1970*1000
if (currentDate - oldDate >= 7200000){
print("greater or equal than two hour") //"greater or equal than two hour"
} else {
print("smaller than two hour")
}
if difference is less than 2 hours
let currentDate = Date().timeIntervalSince1970*1000
let calendar = NSCalendar.current
let yesteraysDate = calendar.date(byAdding: .hour, value: -1, to: Date())
let oldDate = yesteraysDate!.timeIntervalSince1970*1000
if (currentDate - oldDate >= 7200000){
print("greater or equal than two hour")
} else {
print("smaller than two hour") //"smaller than two hour""
}
Upvotes: 1
Reputation: 15512
Try to use more benefits from Swift
. Try to fetch difference in hours with usage of dateComponents
.
let calendar = NSCalendar.current
let hours = calendar.dateComponents([.hour], from: Date(), to: Date())
Upvotes: 1