How to print Firestore timestamp as formatted date and time

前端 未结 13 1827
野性不改
野性不改 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条回答
  •  Happy的楠姐
    2021-02-07 11:56

    You can directly convert the Firestore timestamp object to DateTime like this:

    DateTime myDateTime = (snapshot.data.documents[index].data['timestamp']).toDate();
    

    This will return your Firestore timestamp in the dart's DateTime format. In order to convert your DateTime object you can use DateFormat class from intl package. You can use your obtained DateTime object to get the format of your choice like this:

    DateFormat.yMMMd().add_jm().format(myDateTime);
    

    This code produces an output like this:

    Apr 21, 2020 5:33 PM
    

提交回复
热议问题