Before flagging this question as a duplicate, please read on.
I need to compare two NSDate
s. A date I get from a server with the current date.
From NSDate.date()
I get this date 2014-09-25 12:48:23 +0000 which is wrong (the time part). I needed to add 5 hours to get the current time so I did the following.
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss ZZZZZ"
let dateString = formatter.stringFromDate(NSDate.date())
The result is the correct date - 2014-09-25 06:21:56 +05:30
But there's a little hitch. This date is a String, not a NSDate
. I need it to be a NSDate
object to compare it with another date.
I tried converting it back like this,
let date = formatter.dateFromString(dateString)
And I get a wrong result - 2014-09-25 00:55:53 +0000. I tried passing the date string to a new NSDateFormatter
to see if that works but again I still I get the wrong date.
My question is, how can I convert this date string to a NSDate
object which also retains the correct time.
Thank you.