1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How to convert Firestore timestamp to ISO 8601 in python?

Discussão em 'Python' iniciado por Stack, Setembro 14, 2024.

  1. Stack

    Stack Membro Participativo

    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')),

    Continue reading...

Compartilhe esta Página