日期输出为 different.So,无法正确比较

Date output is different.So,can't compare correctly

这是我的code.Please先看看

import UIKit

var expireDate : Double = 1472647093

var date = NSDate(timeIntervalSince1970: expireDate)
print("Date : \(date)")

let current_date = NSDate()

if date.compare(current_date) == NSComparisonResult.OrderedDescending{
    print("is Greater than current date")
}else if date.compare(current_date) == NSComparisonResult.OrderedAscending{
    print("is Less than current date")
}else{
    print("Same")
}

这是操场上的输出:

我真的不知道为什么 "Date : " 输出与日期不同 variable.My 服务器向我发送了一个过期日期格式的 unix 时间戳,它很长 format.And 我真的需要比较它与当前 date.Actually

Aug 31, 2016, 7:08 PM // It has correct day/month/year but incorrect time

2016-08-31 12:38:13 +0000\n // has all correct time and date that the server send which was 12:38PM

那么,为什么我的日期比当前日期晚?为什么是 7:08PM 而不是 12:38PM

有什么帮助吗?

更新:

您没有输入错误的日期,因为 Aug 31, 2016, 7:08 PM 是您当前的时间戳,而印刷品中的时区为 UTC。当您比较日期时,它们都在 UTC 中,因此您没有任何问题。

我上面的问题没有任何问题。我只是误解了我正在处理的时区或标准。服务器已经以 UTC 标准格式向我发送过期日期和我的时区区域设置。

所以,我不需要根据我当前的时区更改过期日期。

我所要做的就是获取我的当前时间将其更改为 UTC 格式使用我当前的时区。并比较服务器 expire_date 和 current_date。任务完成。

// Using Expire Date
var expireDate : NSTimeInterval = 1472689452 // 00:24 AM UTC
var date = NSDate(timeIntervalSince1970: expireDate) // Since January 1 1970 Convert to NSDate

// Then I change this time format to UTC String without adding any timezone because its already included
var df_utc = NSDateFormatter()
df_utc.timeZone = NSTimeZone(name: "UTC")
df_utc.dateFormat = "yyyy.MM.dd G 'at' HH:mm:ss zzz"

// Get the expire date string by formatting it in UTC
var ts_utc_string : NSString = df_utc.stringFromDate(date)

// Getting current time of mine
var local_date = NSDate()
var df_local = NSDateFormatter()
df_local.timeZone = NSTimeZone(name: "MMT") // then use my locale to add that current time UTC [MMT +6:30]
df_local.dateFormat = "yyyy.MM.dd G 'at' HH:mm:ss zzz"
var ts_local_string : NSString = df_local.stringFromDate(local_date)

// Compare
if ts_local_string.compare(ts_utc_string as String) == NSComparisonResult.OrderedDescending{
    print("CurrentTime is Greater than Expire Time")
}else if ts_local_string.compare(ts_utc_string as String) == NSComparisonResult.OrderedAscending{
    print("CurrentTime is Less than Expire Time")
}else{
    print("Same")
}

输出如下:

对于像我一样还在寻找这样问题的人来说,UTC只是时区的标准。如果您需要将它与您的时区进行比较,您只需根据您所在国家/地区的时区更改当前时间即可。