How to print Firestore timestamp as formatted date and time

前端 未结 13 1627
野性不改
野性不改 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 12:09

    You should use fromMillisecondsSinceEpoch function.

    var d = new DateTime.fromMillisecondsSinceEpoch(ts, isUtc: true);
    

    Here ts is int type.

    So we can convert Firebase timestamps to DateTime object as follows.

    DateTime date = DateTime.fromMillisecondsSinceEpoch(timestamp.seconds * 1000);
    

提交回复
热议问题