Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get server Timestamp from firebase v9?

Please, I need your help to get the server timestamp from Firebase version 9.

I've tried to follow the same approach as for Firebase v8 : firebase.firestore.FieldValue.serverTimeStamp() but didn't work for version 9.

Is there any way to do the same thing in Firebase version 9 ?

like image 382
ouakilTechie Avatar asked Sep 01 '25 22:09

ouakilTechie


2 Answers

It's discussed in the documentation. Just import serverTimestamp.

import { updateDoc, serverTimestamp } from "firebase/firestore";

const docRef = doc(db, 'objects', 'some-id');

// Update the timestamp field with the value from the server
const updateTimestamp = await updateDoc(docRef, {
    timestamp: serverTimestamp()
});
like image 99
Doug Stevenson Avatar answered Sep 03 '25 11:09

Doug Stevenson


If you get type error with other answers you can try this:

import { Timestamp} from '@firebase/firestore';

createdAt: Timestamp.fromDate(new Date())
like image 28
LacOniC Avatar answered Sep 03 '25 11:09

LacOniC