I am writing a firebase cloud function in python to read data from firestore database and update it into the firebase realtime database, however I am facing an issue with converting the firestore datetime to ISO 8601 format so i can store the date in a JSON format.
I believe there exists a library to do this in javascript but not in python.
Any help would be greatly appreciated.
import datetime
from google.cloud import firestore
def serialize_datetime(dt):
""" Convert Firestore Timestamp or Python datetime to ISO 8601 string. """
if isinstance(dt, firestore.Timestamp):
return dt.to_pydatetime().isoformat()
elif isinstance(dt, datetime.datetime):
return dt.isoformat()
return dt
'order_datetime': serialize_datetime(order_data.get('orderDate')),
'expiry_datetime': serialize_datetime(order_data.get('deliveryDate')),
Recognized by Google Cloud Collective
New contributor
Nehal AK is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.