无法与日期[本地日期和服务器日期]进行比较

时间:2016-09-30 07:07:20

标签: ios swift nsdate

我试着比较两个日期 -

dateArrayForCompare,是我从NSDate获得的日期,dateArrayServer是我从json响应中获得的日期。

 var dateArrayServer = NSMutableArray()
 var dateArrayCalendar = NSMutableArray()
 var dateArrayForCompare = NSMutableArray()



let dateHomework:NSArray = allAsign.valueForKey("date") as! NSArray
 let homeWork = allAsign.valueForKey("assignmenttype") as! NSArray       

for date in dateHomework{
self.dateArrayServer.addObject(date as! String)
         }


let sys_date = NSDate()
                        print("System Date: \(sys_date)")
                        let df = NSDateFormatter()
                        df.dateFormat = "dd-MM-yyyy"
                        let currentDate = df.stringFromDate(sys_date)
                        print("String Date: \(currentDate)")

                        for dt in 0...self.dateArrayServer.count-1
                        {
                            if(self.dateArrayServer.objectAtIndex(dt) .isEqualToString("\(self.dateArrayForCompare)"))
                            {
                                print("Assignment on date: \(self.dateArrayServer.objectAtIndex(dt)) are:\n\(allAsign.objectAtIndex(dt))")
                            }else
                            {
                                print("\(self.dateArrayServer.objectAtIndex(dt)) doesn't match with \(self.dateArrayForCompare) ")
                            }
                        }

但得到这个结果 -

enter image description here

1 个答案:

答案 0 :(得分:0)

像这样(swift 2.2)

for dt in 0 ... dateArrayServer.count-1
{
    let date1 = dateArrayServer.objectAtIndex(dt) as! String
    for value in dateArrayForCompare.enumerate() {
        if date1 == value.element as! String {
            print(date1)
        }
    }
}