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

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

Explore a common challenge faced by Flutter developers when working with Firestore: printing Firestore Timestamps as formatted date and time in your Flutter app. Firestore, with its powerful NoSQL database capabilities, stores timestamps as specialized objects. However, displaying them in a human-readable format can be a bit tricky. But fear not! In this article, we’ll guide you through the process of converting Firestore Timestamps into formatted date and time strings using Flutter. Whether you’re a beginner or an experienced Flutter developer, join us as we unravel the secrets to printing Firestore Timestamps as beautiful and understandable date and time representations in your Flutter app!

Learn 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.dart.dev/stable/2.17.1/dart-core/DateTime/DateTime.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.

Nirali Patel

Written by Nirali Patel

Nirali Patel is a dedicated Flutter developer with over two years of experience, specializing in creating seamless mobile applications using Dart. With a passion for crafting user-centric solutions, Nirali combines technical proficiency with innovative thinking to push the boundaries of mobile app development.

2 comments

Leave a comment

Your email address will not be published. Required fields are marked *


Discuss Your Project

Connect with Flutter Agency's proficient skilled team for your app development projects across different technologies. We'd love to hear from you! Fill out the form below to discuss your project.

Have Project For Us

Get in Touch

"*" indicates required fields

✓ Valid number ✕ Invalid number

ready to get started?

Fill out the form below and we will be in touch soon!

"*" indicates required fields

✓ Valid number ✕ Invalid number