Swift 3 - Comparing Date objects

后端 未结 14 1540
野的像风
野的像风 2020-11-30 07:14

I\'m updating my app to Swift 3.0 syntax (I know it\'s still in beta but I want to be prepared as soon as it released).

Until the previous Beta of Xcode (Beta 5) I wa

14条回答
  •  半阙折子戏
    2020-11-30 07:37

    SWIFT 3: Don't know if this is what you're looking for. But I compare a string to a current timestamp to see if my string is older that now.

    func checkTimeStamp(date: String!) -> Bool {
            let dateFormatter: DateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            dateFormatter.locale = Locale(identifier:"en_US_POSIX")
            let datecomponents = dateFormatter.date(from: date)
    
            let now = Date()
    
            if (datecomponents! >= now) {
                return true
            } else {
                return false
            }
        }
    

    To use it:

    if (checkTimeStamp(date:"2016-11-21 12:00:00") == false) {
        // Do something
    }
    

提交回复
热议问题