How to print Firestore timestamp as formatted date and time

前端 未结 13 1644
野性不改
野性不改 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:46

    Simple way to solve it.

    //Declare a variable Timestamp and assign the value timestamp from database

    Timestamp timestamp = document['timeFieldName'];
    

    //Use DateTime's parse method to convert back to DateTime

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

    //Use this method to get the H:m of the DateTime. //You can choose the DateFormat you want.

    String readTimeStamp(DateTime date)
      {
        
         var format =new DateFormat.Hm(); // My Format 08:00
    
         return format.format(date);
      }
    

提交回复
热议问题