我可以找到的大多数文档是在Objective-C,我不知道如何在Swift中做。
谢谢您的帮助!! 我喜欢使用扩展,使代码更可读。这里有一些NSDate扩展,可以帮助清理你的代码,使其易于理解。我把它放在sharedCode.swift文件中:
extension NSDate { func isGreaterThanDate(datetoCompare: NSDate) -> Bool { //Declare Variables var isGreater = false //Compare Values if self.compare(datetoCompare) == NSComparisonResult.OrderedDescending { isGreater = true } //Return Result return isGreater } func isLessthanDate(datetoCompare: NSDate) -> Bool { //Declare Variables var isLess = false //Compare Values if self.compare(datetoCompare) == NSComparisonResult.OrderedAscending { isLess = true } //Return Result return isLess } func equalToDate(datetoCompare: NSDate) -> Bool { //Declare Variables var isEqualTo = false //Compare Values if self.compare(datetoCompare) == NSComparisonResult.OrderedSame { isEqualTo = true } //Return Result return isEqualTo } func addDays(daysToAdd: Int) -> NSDate { let secondsInDays: NSTimeInterval = Double(daysToAdd) * 60 * 60 * 24 let dateWithDaysAdded: NSDate = self.dateByAddingTimeInterval(secondsInDays) //Return Result return dateWithDaysAdded } func addHours(hoursToAdd: Int) -> NSDate { let secondsInHours: NSTimeInterval = Double(hoursToAdd) * 60 * 60 let dateWithHoursAdded: NSDate = self.dateByAddingTimeInterval(secondsInHours) //Return Result return dateWithHoursAdded }}
现在如果你可以这样做:
//Get Current Date/Timevar currentDateTime = NSDate()//Get Reminder Date (which is Due date minus 7 days lets say)var reminderDate = dueDate.addDays(-7)//Check if reminderDate is Greater than Right Nowif(reminderDate.isGreaterThanDate(currentDateTime)) { //Do Something...}总结
以上是内存溢出为你收集整理的使用Swift的NSDate比较全部内容,希望文章能够帮你解决使用Swift的NSDate比较所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)