Sam
Sam

Reputation: 144

How to get Firebase serverTime?

For android: We can get serverTime by:

Timestamp.now().getSeconds();

Then for iOS we can get serverTime by:

Timestamp.init().seconds

My problem: Timestamp.now().getSeconds(); is 13 characters long, Timestamp.init().seconds is only 10 characters long.

Is there a way to get the 13 characters serverTime on xCode?

Upvotes: 1

Views: 639

Answers (2)

Sam
Sam

Reputation: 144

I figured it out myself.

Swift has:

ServerValue.timestamp()

Just didn't think of it, for some reason. Only needed it to be written to the server.

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317667

Those lines of code are not getting server time at all. They're getting local time on the device, in the form of a Timestamp object, converted to seconds. There's no API to get server time directly and accurately.

The number of seconds is a number. It's not measured in "number of characters" like a string. I suggest reading the API documentation for Timestamp to better understand the meaning of the methods you're calling, and choose the one that meets your specific needs. You might have to do some conversions to get what you want, so be clear to understand what you're getting.

Upvotes: 2

Related Questions