I added timestamp to Firebase database formatted like:
1969-12-31 23:32:04 +0000
let timestamp = postsArray[indexPath.row].timestamp//query timestamp from Firebase
let date = NSDate(timeIntervalSince1970: timestamp! )//Readable date
let timeSincePost = (NSDate().timeIntervalSinceNow-NSDate().timeIntervalSince(date as Date))//Calculate time since
print("TimeSince", NSDate(timeIntervalSince1970: timeSincePost))
NSDate().timeIntervalSinceNow return value close to
NSDate() — creates date with current timestamp, you can call it
timeIntervalSinceNow here will be difference between time of creating data and method call, miniscule value.
NSDate().timeIntervalSinceNow-NSDate().timeIntervalSince(date as Date)
Can be seen like this:
0 - now.timeIntervalSince(date as Date)
For all dates in past
now.timeIntervalSince(date as Date) is positive, and
0 - positive = negative
And in the end you're subtracting time interval from January 1st, 1970 and get your date of 1969. Btw, why are you doing this?
Also, stop using
NSDate in Swift 3, just use
this method for
how I am able to convert it now to like 5 minutes ago etc...
doesRelativeDateFormatting set to true and get
stringFromDate with original date you got with Firebird timestamp.