Time comparisons in swift

后端 未结 4 1099
梦如初夏
梦如初夏 2020-12-09 20:54

Question:

I need to compare 2 times - the current time and a set one. If the set time is in the future, find out how many minutes remain until said future time.

4条回答
  •  独厮守ぢ
    2020-12-09 21:35

    just to add to @tyt_g207's answer, I found the compare method, but hadn't found NSComparisonResult.OrderedDescending and the others. I used something like the modified below to check an expiration date against today's date

    let date1 : NSDate = expirationDate 
    let date2 : NSDate = NSDate() //initialized by default with the current date
    
    let compareResult = date1.compare(date2)
    if compareResult == NSComparisonResult.OrderedDescending {
        println("\(date1) is later than \(date2)")
    }
    
    let interval = date1.timeIntervalSinceDate(date2)
    

提交回复
热议问题