2016-07-28 75 views
5

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

在什麼時區是你嗎? – Larme

+0

我在希臘,我們有UTC + 02:00 –

+0

在比較它們之前,先打印'currentDate'和'dateevent'。似乎很明顯'self.currentDate'已經改變。順便說一句,你的第一行更容易表達爲'dateevent.compare(self.currentDate)== OrderedDescending' – fishinear

回答

0

我終於找到了我的問題的答案!

我發現它here

我所做的是:

let utcCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) 

裏面viewDidLoad中我PU這樣的:

utcCalendar!.timeZone = NSTimeZone(abbreviation: "UTC")! 

和finnaly我在發言中把這個:

if self.utcCalendar!.isDate等等......

謝謝大家的回答!

編輯!

如果我想打印我必須得改變dateformatter這樣的日期:

dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")! 
1

希臘夏季實際上有與UTC 3小時的時差。所以UTC 2016-07-28 21:00:00 +0000時間和以後實際上是在希臘的第二天。因此,如果您根據當地(希臘)日曆與:

NSCalendar.currentCalendar().isDate(dateevent, equalToDate: self.currentDate, toUnitGranularity: .Day) 

那麼它不會比較相等。

如果你想根據UTC天,而不是來比較,那麼日曆對象的時區設置爲UTC:

NSCalendar *calendar = NSCalender.currentCalendar(); 
calendar.timeZone = NSTimeZone.timeZoneForSecondsFromGMT(0); 
if (calendar.isDate(dateevent, equalToDate: self.currentDate, toUnitGranularity: .Day)) ... 

如果你想根據一些其他時區來比較,那麼你顯然需要設置timeZone有所不同。


如果你是根據當地時區比較的日期確定,但只是迷茫的日期在UTC印刷,然後用下面的轉換爲字符串:

NSDateFormatter().stringFromDate(dateevent) 
+0

和我做了什麼? –

+0

更新了答案 – fishinear

+0

好的我會試試非常感謝你 –

2

我不打算討論你應該在服務器端使用什麼時區,但最方便和一致的方式是UTC。由於您已在服務器上使用UTC時區,因此在服務器上存儲日期和時間時需要考慮這一點。我的意思是,如果你正在存儲例如2016-07-28 21:00:00 +0000,它不一定會在您的位置轉換爲2016-07-28 21:00:00

參見以下:

let time = NSDate()  // Create an object for the current time. 

print(time, "\n")   // Sample output: 
           2016-07-28 15:23:02 +0000 

打印出來,因爲它是該time對象輸出在UTC當前時間。作爲參考,我的本地時區也正好是UTC + 3,因此當地時間爲2016-07-28 18:23:02 +0300

讓我們來看看在字符串格式未來幾日期:

let strings = [ 
    "2016-07-28 19:00:00 +0000", 
    "2016-07-28 20:00:00 +0000", 
    "2016-07-28 20:59:59 +0000", // Second before midnight, UTC+3. 
    "2016-07-28 21:00:00 +0000", // Midnight in UTC+3. 
    "2016-07-28 22:00:00 +0000", 
    "2016-07-28 23:30:00 +0000", 
    "2016-07-28 23:59:59 +0000", // Second before midnight, UTC. 
    "2016-07-29 00:00:00 +0000" // Midnight in UTC. 
] 

現在,讓我們轉換這些字符串爲NSDate對象,再次將留在UTC時區:

var dates: [NSDate] = [] 

for string in strings { 
    let formatter = NSDateFormatter() 
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z" 
    guard let date = formatter.dateFromString(string) else { continue } 
    dates.append(date) 
} 

接下來,我們將NSDate對象轉換爲本地格式的字符串,這可能是您的困惑所在:

for date in dates { 
    print("Current time in UTC: ", time) 
    print("Date in UTC:   ", date) 

    let formatter = NSDateFormatter() 
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" 
    formatter.timeZone = NSTimeZone.localTimeZone() 
    print("Current local time: ", formatter.stringFromDate(time)) 
    print("Date in local time: ", formatter.stringFromDate(date)) 
} 

// Sample output for the date 2016-07-28 19:00:00 +0000: 
// Current time in UTC: 2016-07-28 15:23:02 +0000 
// Date in UTC:   2016-07-28 19:00:00 +0000 
// Current local time: 2016-07-28 18:23:02 
// Date in local time: 2016-07-28 22:00:00 
+0

非常好的解釋我試着用這個作爲上面的答案,我會告訴你我的結果謝謝 –