97

I know you can pull the server timestamp in web, ios, and android - but what about the new Cloud Functions for Firebase? I can't figure out how to get the server timestamp there? Use case is me wanting to timestamp an email when it arrives.

On web it is Firebase.database.ServerValue.TIMESTAMP

But that doesn't seem to be available in the functions node server interface?

I think it is late and I may be missing the point here...

EDIT

I am initializing like this

admin.initializeApp(functions.config().firebase);
const fb = admin.database()

Then, it is being called like this..

Firebase.database.ServerValue.TIMESTAMP

But, that is from a client side integration. On Functions, Firebase isn't initialized like this. I've tried

admin.database().ServerValue.TIMESTAMP

and

fb.ServerValue.TIMESTAMP
2

9 Answers 9

136

Since you're already using the admin SDK, the correct syntax to write a server-side timestamp to the Realtime Database is:

admin.database.ServerValue.TIMESTAMP

This returns a so-called sentinel value, which the database server recognizes as an instruction to write the current timestamp.


If you're using Cloud Firestore (instead of the Realtime Database that OP asked about here), the correct incantation is:

admin.firestore.FieldValue.serverTimestamp()

If that doesn't work for you, be sure to check the reference docs for the latest updates and examples.

9
  • 1
    This is the simple answer I was looking for - thanks @frank-van-puffen May 4, 2017 at 15:23
  • 18
    how do you print timestamp in milliseconds with this? admin.database.ServerValue.TIMESTAMP returns an object { '.sv': 'timestamp' }, which I believe is a placeholder
    – vir us
    Dec 17, 2017 at 23:08
  • 4
    It is indeed a placeholder. To get the actual value, listen on the location that you write to. Dec 18, 2017 at 1:45
  • 1
    yes, if the server local time is out of sync.
    – DIGI Byte
    Jan 18 at 5:14
  • 1
    @ShaunRamsey There are no updates to the syntax of writing server-side timestamps to the Realtime Database, which is what this question is about. I added an example for Firestore (which wasn't even out yet when I posted this 🙂) to cover your use-case too, since you're likely not the only dev who lands here while looking for Firestore information. Mar 11 at 22:38
123

If you use the Firebase Admin SDK, here is the correct syntax:

2023

// Cloud Firestore
import { Timestamp } from 'firebase-admin/firestore'; 

// usage
Timestamp.now()

(2021-2022)

// Cloud Firestore
admin.firestore.Timestamp.now();

(Checked 2019-08-27):

const admin = require('firebase-admin');
// or
// import * as admin from 'firebase-admin';

// Using Cloud Firestore
admin.firestore.FieldValue.serverTimestamp()

// Using Realtime Database
admin.database.ServerValue.TIMESTAMP
6
  • 1
    Working like a charm :-) Thanks.
    – Qorbani
    Jan 28, 2018 at 6:49
  • 5
    I think for cloud firestore is has to be: admin.firestore.FieldValue.serverTimestamp()
    – ThdK
    Jul 28, 2019 at 11:51
  • 1
    firestore.FieldValue.serverTimestamp() is not working on nodejs firebase functions. Mar 10, 2020 at 14:40
  • 14
    2021: admin.firestore.Timestamp.now() Oct 8, 2021 at 14:16
  • 4
    2023: import { Timestamp } from 'firebase-admin/firestore'; Timestamp.now()
    – CTOverton
    Jan 29 at 22:20
27

Just to clarify for future readers:

admin.database.ServerValue.TIMESTAMP returns a non-null Object and is a placeholder value for auto-populating the current timestamp. It doesn't contain the actual timestamp. The database will replace this placeholder when it will execute the command.

If you are using it inside a database.ref then it works just as you expect and is the preferred way to enter a timestamp :

var sessionsRef = firebase.database().ref("sessions");
sessionsRef.push({
startedAt: firebase.database.ServerValue.TIMESTAMP // this will write 'startedAt: 1537806936331`
});

But if you try to use it outside the database function (for example to return the time now or make some calculations) it will return an object that you cannot use it:

console.log(firebase.database.ServerValue.TIMESTAMP) // this will return an [object Object]

See more about it in firebase.database.ServerValue and in this SO question.

Date.now() works just fine outside a database function if you want to use it for a calculation or any other general use.

console.log(Date.now()); // this will return 1537806936331

Both of them are using unix time which is number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, and it is irrelevant from the timezone. It is the same number on client and on server (...or almost:-). See unix time .

3
  • 1
    Best answer for a newbie! :)
    – John T
    Apr 30, 2019 at 21:43
  • hello, when i write timestamp and save it into DB i got this in cloud functions message: 'Messaging payload contains an invalid value for the "notification.timestamp" property. Values must be strings.' @SpiralOut
    – DevAS
    Jul 24, 2019 at 0:36
  • @DevAS Try posting a new question with more details about your code. But Notification.timestamp is not firebase db keyword. Maybe use firebase.database.ServerValue.TIMESTAMP
    – Spiral Out
    Jul 25, 2019 at 11:47
16

I'm new to node.js myself, but Date.now() works in my tests.

Edit

I misunderstood you question--didn't realize you wanted to timestamp data you were storing in the Firebase database. I thought you simply wanted to get the time on the server that was running your cloud function. If you want to timestamp a received email being stored in the Firebase database, then using admin.database.ServerValue.TIMESTAMP is without question the best approach.

Just for my own education, I wrote the following function to see how the times compare. I would expect the times on the cloud function server and database server are synced to a very accurate time reference. When I run this function, the database timestamp typically within a hundred milliseconds of the Date.now() value. The database timestamp being a little later is reasonable, given that it takes the cloud function some time to connect to the database and perform the write.

exports.timeTest = functions.database.ref('/test/trigger')
    .onWrite(event => {

        const now= Date.now();
        console.log('now=', now);

        const timeVals = {
          dateNow : now,
          serverTimestamp : admin.database.ServerValue.TIMESTAMP
        };

        return event.data.ref.parent.child('times').update(timeVals);
    });
3
  • And does Date.now() get the node server time, or the time from the client's computer to do it's calculation? Or does it use some standardized clock? If I set my computer time to one time, did Date.now() then set it to a different time and did Date.now() would I get two different results? Apr 23, 2017 at 22:34
  • 1
    @JustinHandley: Updated my answer. Date.now() is the time on the server running your cloud function. It is completely independent of the time on any client computer.
    – Bob Snyder
    Apr 24, 2017 at 1:35
  • 3
    admin.database.ServerValue.TIMESTAMP is the only acceptable solution when you are using Firebase Rules to test that this == now (Bolt) Sep 18, 2018 at 23:21
16

Depends on use case

case 1

you want to set document field to server timestamp

Example can be

user {
  email: "example.com",
  lastUpdated: admin.firestore.FieldValue.serverTimestamp(),
}

Note serverTimestamp returns a non-null Object and is a placeholder value for auto-populating the current timestamp. It doesn't contain the actual timestamp. The database will replace this placeholder when it will execute the command

*Returns a sentinel used with set(), create() or update() to include a server-generated timestamp in the written data.

@return The FieldValue sentinel for use in a call to set(), create() or update().*

case 2

you want use server time stamp for your functions logic

if (currentTime == 6pm) // TODO: send push notifications
else // TODO: do nothing

for that you might want to do something like

admin.firestore.Timestamp.now() or admin.firestore.Timestamp.fromDate(new Date())

good reads: https://bigcodenerd.org/firebase-server-timestamp-cloud-functions/

9

Using the following in the cloud function which is equivalent of Timestamp.now() on client side, this returns the current Timestamp

admin.firestore.Timestamp.now()

But if you want to initialise Timestamp from Date object you can do it as follows

admin.firestore.Timestamp.fromDate(new Date())

And if you want to to initialise Timestamp for future or past date then first intialise the Date object either from parsing string or setting time that you want to set and pass it to Timestamp.fromDate()

var date = new Date('Wednesday, October 30, 2019 09:10 PM')
//or date = new Date('2014-06-30T06:40:53+05:30')
var timestamp = admin.firestore.Timestamp.fromDate(date)
2

It's in the documentation August 16, 2020.

https://cloud.google.com/firestore/docs/manage-data/add-data#server_timestamp

// Get the `FieldValue` object
const FieldValue = admin.firestore.FieldValue;

// Create a document reference
const docRef = db.collection('objects').doc('some-id');

// Update the timestamp field with the value from the server
const res = await docRef.update({
  timestamp: FieldValue.serverTimestamp()
});

Here's how I used it in my code:

const createUserDoc = (user) => {
    const FieldValue = admin.firestore.FieldValue
    return db.collection('users').doc(user.uid).set({
        memberSince: FieldValue.serverTimestamp(),
    })
}
1

You can try:

const timestamp = admin.firestore.Timestamp.now();

-2
console.log("Show current timestamp, in Milliseconds= " + Date.now());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.