How to print Firestore timestamp as formatted date and time

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

    Once you've got a timestamp back from Firestore, something like

    Timestamp(seconds=1560523991, nanoseconds=286000000)

    you need to parse it into an object of type DateTime:

    DateTime myDateTime = DateTime.parse(timestamp.toDate().toString());

     print('$myDateTime');
    

    This will give you something like:

    2020-05-09 15:27:04.074

    You can then format myDateTime like this:

    String formattedDateTime =
              DateFormat('yyyy-MM-dd – kk:mm').format(myDateTime);
    
    print('$formattedDateTime');
    

    That will give you:

    2020-05-09 – 15:27

提交回复
热议问题