Firestore Python: Use Server Timestamp and Timestamp.now()

May 30, 2019

For Firestore Database Server timestamp, use firebase_admin.firestore.SERVER_TIMESTAMP

For local timestamp, use datetime.datetime.now() (assuming your local machine/server is setup with UTC timezone) or datetime.datetime.utcnow().

import datetime# from google.cloud.firestore_v1 import SERVER_TIMESTAMP# from google.cloud.firestore import SERVER_TIMESTAMPfrom firebase_admin.firestore import SERVER_TIMESTAMPdoc_ref = db.collection('test').document()doc_ref.set({    'created': SERVER_TIMESTAMP,    'local_created': datetime.datetime.now(),    'name': 'Test'    })

NOTE: Kotlin have a special Timestamp class for Firestore timestamp, but datetime module is sufficient for Python.

NOTE: Refer Setup Firestore on Cloud Functions (Python) for db instance.

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.