9

According to answer below, Firebase-Admin is required to use FieldValue.serverTimestamp().

How do I get the server timestamp in Cloud Functions for Firebase with Firestore? .

But I am using only Firebase JS and Firebase Firestore JS. The application runs only in client side. However, Firebase-Admin requires a server.

Is there any way to use server timestamp when doing a write operation ?

3
  • 4
    FieldValue.serverTimestamp() available in all client SDKs, including Javascript.
    – Bob Snyder
    Oct 24, 2017 at 15:06
  • @BobSnyder Thanks. It worked.
    – le0
    Oct 24, 2017 at 16:00
  • yeah using this solves it in your object to add: firebase.firestore.FieldValue.serverTimestamp() Aug 4, 2018 at 11:14

4 Answers 4

5

https://firebase.google.com/docs/reference/js/firestore_.md#servertimestamp

import { serverTimestamp } from "firebase/firestore";

addDoc(collection(db, 'your_collection'), {
  ...
  createdAt: serverTimestamp()
})
0
3

you can get the instance from your firebase instance:

firebase.firestore.FieldValue.serverTimestamp();
1
  • 4
    What can I use now because, firebase.firestore.FieldValue.serverTimestamp() not working now on web Jan 6, 2022 at 16:32
0

try to import firebase like this -

import firebase from "firebase/compat/app";
-3

This answer is outdated, check above answer!

Simply use Firebase.ServerValue.TIMESTAMP within your object.

Like this:

ref.set({ time: Firebase.ServerValue.TIMESTAMP })
4
  • What is the difference with firebase.firestore.FieldValue.serverTimestamp()?
    – wonsuc
    Jul 27, 2019 at 12:16
  • @wonsuc this answer is related to the realtime database. Your code works for firestore.
    – Orlandster
    Jul 28, 2019 at 9:13
  • 2
    Sorry, I had to downvote, since OP is talking about Firestore.
    – wonsuc
    Jul 28, 2019 at 12:04
  • 1
    This should in fact be not the accepted answer, since it confuses people. @Leonardo
    – akauppi
    Mar 25, 2020 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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