當我將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