How to print Firestore timestamp as formatted date and time

前端 未结 13 1823
野性不改
野性不改 2021-02-07 11:07

My timestamp returns Timestamp(seconds=1560523991, nanoseconds=286000000) in a Flutter Firestore snapshot.

I want to print it as properly formatted date and

13条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-07 11:58

    When we push the DateTime object to Firestore, it internally converts it to it's own timestamp object and stores it.

    Method to convert it back to Datetime after fetching timestamp from Firestore:

    Firestore's timestamp contains a method called toDate() which can be converted to String and then that String can be passed to DateTime's parse method to convert back to DateTime

    DateTime.parse(timestamp.toDate().toString())
    

提交回复
热议问题