Varis Darasirikul
Varis Darasirikul

Reputation: 3063

Got wrong date from FirebaseServerValue.timestamp() by Swift

Now i'm working on IOS app by swift with Firebase.com as back end

First i tried to put the time by this function

var userHistoryForupdate = ["dealdate":FirebaseServerValue.timestamp()]

userHistoryRef.updateChildValues(userHistoryForupdate as [NSObject : AnyObject], withCompletionBlock: {
    (error:NSError?, ref:Firebase!) in
    if (error != nil) {

    } else {

    }
})

And then in the other views i retrieve the time by this function

var tempPubdate = self.pubDataArray[indexPath.row]["dealdate"] as! Double

dateFormatter.dateFormat = "dd MMM yy"

var dealDateAndTime = NSDate(timeIntervalSince1970: tempPubdate)

cell?.historyDate.text = "\(self.dateFormatter.stringFromDate(dealDateAndTime))"

And my cell display

enter image description here

As you see i got 23 JAN 38 not today.

Anyone know how can i get the correct Date from firebase Thanks!

Upvotes: 2

Views: 668

Answers (2)

Kratos
Kratos

Reputation: 339

timeIntervalSince1970 takes seconds, while the timestamp from firebase is in milliseconds. You can solve it ju by dividing by 1000

let date = NSDate(timeIntervalSince1970: timeStamp / 1000 )

Upvotes: 0

Mathew Berg
Mathew Berg

Reputation: 28750

timeIntervalSince1970 takes seconds, while the timestamp from firebase is in milliseconds.

Upvotes: 3

Related Questions