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
AL.
  • 36,815
  • 10
  • 142
  • 281
Justin Handley
  • 1,159
  • 1
  • 7
  • 14
  • As far as I know `ServerValue.TIMESTAMP` is equally available in Cloud Functions for Firebase. Can you share the [minimal code that reproduces the problem](http://stackoverflow.com/help/mcve)? – Frank van Puffelen Apr 23 '17 at 14:19
  • Hi @FrankvanPuffelen - I edited the original with an example of my initialization and what isn't working. – Justin Handley Apr 23 '17 at 22:32

10 Answers10

139

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    This is the simple answer I was looking for - thanks @frank-van-puffen – Justin Handley May 04 '17 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 '17 at 23:08
  • 4
    It *is* indeed a placeholder. To get the actual value, listen on the location that you write to. – Frank van Puffelen Dec 18 '17 at 01:45
  • Can this be different from the value in a regular JS Date? – Maduranga E May 06 '20 at 22:07
  • 1
    yes, if the server local time is out of sync. – DIGI Byte Jan 18 '23 at 05:14
  • The next answer is being updated to keep up with the latest updates related to timestamps. Using this I was left with a .sv timestamp as well (including many versions based on fieldvalue). See below for updated syntaxes. – Shaun Ramsey Mar 11 '23 at 22:04
  • 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. – Frank van Puffelen Mar 11 '23 at 22:38
  • This makes so much more sense. Problematically, the approaches listed all are stubbed out even when just interacting with firestore instead of realtime db. Thanks Frank! – Shaun Ramsey Mar 12 '23 at 00:54
  • I may eventually do that as I like the idea of the server setting the value on set. But, the now method listed in the comment by CTOverton to the answer below solved it for me for the moment: `import { Timestamp } from 'firebase-admin/firestore'; Timestamp.now();` I voted you up - no worries. When I get some time, I'll do other serverTimestamp tests to see if, perhaps, I was just uncarefully mixing firestore and database or not actually calling the function on my set and instead just used the function prototype or some other careless error. If not, I'll make a post. – Shaun Ramsey Mar 12 '23 at 06:34
126

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
Boern
  • 7,233
  • 5
  • 55
  • 86
Paul
  • 2,430
  • 3
  • 15
  • 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 .

Spiral Out
  • 1,045
  • 12
  • 18
  • 1
    Best answer for a newbie! :) – John T Apr 30 '19 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 '19 at 00: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 '19 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);
    });
Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
  • 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? – Justin Handley Apr 23 '17 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 '17 at 01:35
  • 3
    admin.database.ServerValue.TIMESTAMP is the only acceptable solution when you are using Firebase Rules to test that `this == now` (Bolt) – Luke Pighetti Sep 18 '18 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/

sultanmyrza
  • 4,551
  • 1
  • 30
  • 24
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)
ked
  • 2,426
  • 21
  • 24
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(),
    })
}
Collin Thomas
  • 1,590
  • 1
  • 13
  • 12
1

You can try:

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

Shiva Yadav
  • 72
  • 1
  • 9
0

Use

const new_date = new Date()
console.log('time',new_date.getTime())

For more details click here

Durgesh Kumar
  • 935
  • 10
  • 17
-3
console.log("Show current timestamp, in Milliseconds= " + Date.now());
Gene
  • 10,819
  • 1
  • 66
  • 58