Search code examples
firebase-realtime-databasefloating-pointdouble

Firebase - what data type is firebase.database.ServerValue.TIMESTAMP


I was looking around to find the data type of a TIMESTAMP. I looked in the docs but it doesn't mention anything about type.

Are these a Float or a Double or another type?

const currentDate = Date.now();
const timeStamp = firebase.database.ServerValue.TIMESTAMP;

This is the way firebase.database.ServerValue.TIMESTAMP appears inside the RTDB:

enter image description here


Solution

  • The ServerValue.TIMESTAMP field is a so-called sentinel value. Upon writing it to the database, it is actually a map: { ".sv": "timestamp" }. But then in the database itself, and when you read a field of this value back, you'll get a regular number (the milliseconds since the epoch).

    Depending on your platform, this asymmetrical type can lead to interesting programming challenges. For example, see: When making a POJO in Firebase, can you use ServerValue.TIMESTAMP?