如何在 Swift iOS 8 中设置 NSDate > NSDate?

How to set NSDate > NSDate in Swift iOS 8?

我正在尝试仅加载在当前时间 24 小时内创建的帖子。我在设置 NSDate < NSDate 的部分有问题,但由于 NSDate 不是 Int,我不知道完成相同任务的另一种方法。感谢任何帮助!

override func viewDidAppear(animated: Bool) {
    var annotationQuery = PFQuery(className: "Post")
    currentLoc = PFGeoPoint(location: MapViewLocationManager.location)
    //annotationQuery.whereKey("Location", nearGeoPoint: currentLoc, withinMiles: 10)
    annotationQuery.whereKeyExists("Location")
    annotationQuery.findObjectsInBackgroundWithBlock {
        (points, error) -> Void in
        if error == nil {
            // The find succeeded.
            println("Successful query for annotations")
            // Do something with the found objects

            let myPosts = points as! [PFObject]

            for post in myPosts {
                let point = post["Location"] as! PFGeoPoint
                let annotation = MKPointAnnotation()
                annotation.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude)
                annotation.title = post["title"] as! String!
                let annotationSubTitleUser = post["username"] as! String!

                let formatter = NSDateFormatter()
                formatter.dateStyle = NSDateFormatterStyle.ShortStyle
                formatter.timeStyle = .ShortStyle
                let dateString = formatter.stringFromDate(post.createdAt!)


                var current = NSDate()
                var expireDate = NSDate(timeIntervalSinceNow: 60 * 60 * 24)

                annotation.subtitle = "User: \(annotationSubTitleUser)  Time: \(dateString)"


                //Here is where I am having issues setting current < expireDate
                if (current < expireDate) {
                    self.mapView.addAnnotation(annotation)
                }
            }

        } else {
            // Log details of the failure
            println("Error: \(error)")
        }
    }

}

使用:

if (current.timeIntervalSince1970() < expireDate.timeIntervalSince1970()) {
    self.mapView.addAnnotation(annotation)
}

相反。

如果我们尝试比较日期,NSDate 有一个 compare 方法接受另一个 NSDate 参数和 returns 一个 NSComparisonResult.

if date1.compare(date2) == .OrderedDescending {
    // date1 comes after date2
}

if date1.compare(date2) == .OrderedAscending {
    // date1 comes before date2
}

if date1.compare(date2) == .OrderedSame {
    // date1 and date2 are the same
}

使用此代码..

 let interval = dateOne!.timeIntervalSinceDate(dateTwo!)
        var numberOfDays = interval/(3600*24)

        if (numberOfDays == 0) {
            println("same day") //dateOne = dateTwo
        }else if (numberOfDays>0){
            println("two > one") //dateOne > dateTwo
        }else if (numberOfDays<0){
            println("two < one") //dateOne < dateTwo
        }

这可能对你有帮助:)

如果您想确定两个日期之间较早或较晚的日期,那么 NSDate class 可以为您提供很多帮助,因为它提供了两个名为 [=13= 的方法] 和 laterDate 分别。使用任何这些方法时的语法都很简单:

date1.earlierDate(date2)

这是它的工作原理:

如果 date1 对象早于 date2,则上述方法将 returndate1 的值。 如果 date2 对象早于 date1,则 date2 的值将被 returned。 如果日期相等,则 date1 再次 returned。 以上所有内容也适用于 laterDate

第二种比较两个NSDate对象的方法涉及使用NSDateclass的compare方法和NSComparisonResult枚举

// Comparing dates - Method #2
if date1.compare(date2) == NSComparisonResult.OrderedDescending {
    print("Date1 is Later than Date2")
}
else if date1.compare(date2) == NSComparisonResult.OrderedAscending {
    print("Date1 is Earlier than Date2")
}
else if date1.compare(date2) == NSComparisonResult.OrderedSame {
    print("Same dates")
}

你可以从这里了解更多Link.

乐于助人。 :)