Thiha Aung
Thiha Aung

Reputation: 5096

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

Here is my code.Please take a look first

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")
}

Here is the output on playground : enter image description here

I really don't know why "Date : " output is different than date variable.My server send me an expire date format in unix timestamp which is long format.And I really need to compare it with current 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

So,why I am having greater than current date?And why it was 7:08PM instead of 12:38PM

Any help?

UPDATE : enter image description here

Upvotes: 0

Views: 84

Answers (2)

Thiha Aung
Thiha Aung

Reputation: 5096

My above question has nothing wrong with it. I'm just misunderstood with the timezone or standard that I am dealing with. The server already send me expire date with my timezone locale in UTC standard format.

So,I don't need to change that expire date according to my current time zone.

All I have to do is get my current time,change it to UTC format using my current timezone. And compare server expire_date and current_date. Mission Accomplish.

// 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")
}

Here is the output : enter image description here

For those who are still seeking for such problem like me, UTC was just a standard for timezone. If you need to compare it with your timezone, you just need to change your current time according to your country timezone.

Upvotes: 0

Marco Santarossa
Marco Santarossa

Reputation: 4066

You don't have wrong dates since Aug 31, 2016, 7:08 PM is your current timestamp, whereas in the print you have with timezone UTC. When you compare the dates they are both in UTC so you don't have any problems.

Upvotes: 2

Related Questions