How to Print Firestore Timestamp as Formatted Date and Time In Flutter ??

· 4 min read
How to Print Firestore Timestamp as Formatted Date and Time In Flutter
How to Print Firestore Timestamp as Formatted Date and Time In Flutter

When users trying to send a notification from Rest Api using Firebase Admin SDK to the Flutter Application and it seems it requires a device token to send a notification. In this article, How to Print Firestore Timestamp as Formatted Date and Time In Flutter ??

How to Print Firestore Timestamp as Formatted Date and Time In Flutter ??

You will get a Unix timestamp from firestore even if you send a DateTime to firestore. You can parse a DateTime from Firestore with

DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);

The DateTime class has two options to return a string. toIso8601String() or toString() choose the one you need. Or use eg. DateTime.now().hour; to get our and create your own output.

For more information: Check https://api.dartlang.org/stable/2.4.0/dart-core/DateTime-class.html

Timestamp parameter is the time in seconds

String formatTimestamp(int timestamp) {
      var format = new DateFormat('d MMM, hh:mm a');
      var date = new DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
      return format.format(date);
    }

Please check this answer for intl date formats

When we push the DateTime object to Firestore, it internally converts it to its 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())

Firestore will return TimeStamp like Timestamp(seconds=1560523991, nanoseconds=286000000).

This can be parsed as

Timestamp t = document['timeFieldName'];
DateTime d = t.toDate();
print(d.toString()); //2019-12-28 18:48:48.364

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 the 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

First, visit here for intl package, then paste the package into your pubspec.yaml, run pub get the usual way of updating pubspec. Import the package into your dart file and try out the below method:

String timestamp;

DateTime now = DateTime.now();
String formatDate =
    DateFormat('yyyy-MM-dd – kk:mm').format(now);
timestamp = formatDate;

You can also try below:

You should use fromMillisecondsSinceEpoch function.

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

Here ts is the int type.

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

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

Conclusion:

Thanks for Reading !!!

In this article, we have been through how to print firestore timestamp as formatted Date and Time in flutter ??

Do let us know if you need any assistance with flutter development. !!! We would love to assist you.

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.

2 comments

Leave a Reply