当我将currentDate与date进行比较时,标题说明了一个非常奇怪的反应。 这是发生了什么事情: 我做了一个查询,我的服务器采取一些日期,然后我打印它们,所以我会确保它把他们正确(并且一切都很好)。 然后我打印出3小时前出现的currentDate。 好的,我会解决它,然后我说。 但是!当我试图比较它们时,我只采用20:59和更早的日期。与currentDate比较日期时出现奇怪的反应
这是我的代码(//dateevent are the dates that I recover from server
)
if dateevent.earlierDate(self.currentDate).isEqualToDate(self.currentDate){
print("All dates \(dateevent)")
if NSCalendar.currentCalendar().isDate(dateevent, equalToDate: self.currentDate, toUnitGranularity: .Day){
print("here is what it passed from server \(dateevent)")
print("here is the current date \(self.currentDate)")
}
}
这是我的输出
All dates 2016-07-28 19:00:00 +0000
here is what it passed from server 2016-07-28 19:00:00 +0000
here is the current date 2016-07-28 13:43:51 +0000
All dates 2016-07-28 19:00:00 +0000
here is what it passed from server 2016-07-28 19:00:00 +0000
here is the current date 2016-07-28 13:43:51 +0000
All dates 2016-07-28 21:00:00 +0000
All dates 2016-07-28 21:00:00 +0000
All dates 2016-07-28 23:30:00 +0000
All dates 2016-07-29 21:00:00 +0000
All dates 2016-07-29 22:30:00 +0000
All dates 2016-07-29 23:00:00 +0000
All dates 2016-07-29 23:00:00 +0000
All dates 2016-07-29 23:30:00 +0000
All dates 2016-07-30 21:00:00 +0000
在什么时区是你吗? – Larme
我在希腊,我们有UTC + 02:00 –
在比较它们之前,先打印'currentDate'和'dateevent'。似乎很明显'self.currentDate'已经改变。顺便说一句,你的第一行更容易表达为'dateevent.compare(self.currentDate)== OrderedDescending' – fishinear