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_TIMESTAMP
from firebase_admin.firestore import SERVER_TIMESTAMP

doc_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.

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.