2016-08-19 81 views
0

我在sqlite數據庫中保存了一個日期。知道我嘗試獲得小時和分鐘。從NSDate獲取小時數

Europe/Berlin (GMT+2) offset 7200 (Daylight) 
2016-08-17 18:44:57 +0000 
the hour is 20 and minute is 44 
2016-08-18 18:44:57 +0000 
the hour is 20 and minute is 44 
2016-08-19 15:44:57 +0000 
the hour is 17 and minute is 44 
2016-08-18 16:44:57 +0000 
the hour is 18 and minute is 44 
2016-08-17 18:44:57 +0000 
the hour is 20 and minute is 44 
2016-08-18 18:44:57 +0000 
the hour is 20 and minute is 44 
2016-08-19 15:44:57 +0000 
the hour is 17 and minute is 44 
2016-08-18 16:44:57 +0000 
the hour is 18 and minute is 44 

的時區是正確的:就是工作時間由2

print(calendar.timeZone) 
while result.next() { 
       var hour = 0 
       var minute = 0 
       let calendar = NSCalendar.currentCalendar() 
       if #available(iOS 8.0, *) { 
        calendar.getHour(&hour, minute: &minute, second: nil, nanosecond: nil, fromDate: result.dateForColumn("time")) 
        print(result.dateForColumn("time")) 
        print("the hour is \(hour) and minute is \(minute)") 
       } 

      } 

我得到以下輸出偏移。我嘗試了另外兩種解決方案。但它總是同樣的問題。

+1

沒有問題,輸出是正確的。 'print(someDate)'用UTC打印,日期組件在你的本地時區。 –

回答

1

result.dateForColumn("time")UTC因爲你有+0000而第二個輸出是在另一個時區(Europe/Berlin (GMT+2)),所以日期是一樣的。