0

I'm trying to get the server time from the firebase with the following code:

let timestamp = ServerValue.timestamp()
let today = NSDate(timeIntervalSince1970: timestamp/1000)

But this gives me an error saying:

Binary operator '/' cannot be applied to operands of type '[Any Hashable: Any]' and 'Int'

Why ServerValue.timestamp() dosen't return a TimeIntervel value? How can I get the server's local time?

Difeng Chen
  • 159
  • 3
  • 10
  • Whats is server value ? Can you share it ? – Sagar Chauhan Mar 21 '18 at 16:51
  • As per your error it's may be dictionary. right ? – Sagar Chauhan Mar 21 '18 at 16:53
  • Take a look to this question: https://stackoverflow.com/questions/29243060/trying-to-convert-firebase-timestamp-to-nsdate-in-swift – Adolfo Mar 21 '18 at 16:54
  • @Adolfo I already read this before I posted. But that question was posted two years ago, and I tried the solutions. – Difeng Chen Mar 21 '18 at 17:00
  • You’ll need to pull it out of the dictionary before you can operate on it. We need to see the actual return value of `ServerValue.timestamp()` to show you want you need to do exactly. – SirCJ Mar 21 '18 at 17:33
  • The link @Adolfo provided above is a great link and contains almost the exact code you need to write a Firebase timestamp. The only thing to add would be the code to observe the node the timestamp was written to. I just copy and pasted that code into a project and it worked perfectly. You may also want to see my answer [here](https://stackoverflow.com/questions/43203910/how-to-create-negative-firebase-timestamp-in-swift/43379902#43379902) which is not directly related but may provide some additional insight into timestamps and Firebase. – Jay Mar 21 '18 at 20:50

2 Answers2

1

You can't use the ServerValue.TIMESTAMP to get the Firebase's server time.

It maps to the Firestore timestamp value for the server time when you write it to a document.

If you want the server time, there are two ways.

  1. Write the ServerValue.TIMESTAMP to a temporary doc in Firestore and read it from there.

  2. Use a Google Cloud function to get an instance of Date() object as a string in response.end() method. Create an HTTP endpoint like this. You can then use an AJAX request to get the result.

const app = (req, res) => { // this will return the Firebase server time res.send(new Date()); };

Utkarsh Bhatt
  • 1,536
  • 2
  • 14
  • 23
0

I'm not sure how to get the timestamp from servervalue but the error is being thrown because the convenience init(timeIntervalSince1970 secs: TimeInterval) you are using to create the date is expecting the secs to be in TimeInterval which is Double and to pass that you are trying to divide a type which is not permitted (timestamp/1000).

Below is an example of how your double value should be to get it working

let unixTimestamp = 1480134638.0
let date = Date(timeIntervalSince1970: unixTimestamp)

print(date) -> "2016-11-26 04:30:38 +0000\n"
Shivakumar
  • 110
  • 1
  • 6
  • Unfortunately this information has nothing to do with the question or the error; that error is indicating the division symbol '/' cannot be used on a Dictionary [Any Hashable: Any] divided by an Int. However, your comment about Double is on point. – Jay Mar 21 '18 at 20:54