2016-07-28 80 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

非常好的解释我试着用这个作为上面的答案,我会告诉你我的结果谢谢 –